Solved

Problem with find command in linux

Posted on 2014-09-11
10
648 Views
Last Modified: 2014-09-12
I am trying to the root directory and the directories immediately below that directory.
Also I need to exclude some directories such as /proc

Here is my attempt to get this to work  

for i  in `find /   ! -path /proc\*   -maxdepth 1  ` 
do
echo $i
done

If I put -maxdepth 1 first I do not exclude /proc
If I put  ! -path /proc\* first I do not get the maxdepth of just 1

Redhat Linux 5   
0
Comment
Question by:TIMFOX123
10 Comments
 
LVL 4

Assisted Solution

by:exploitedj
exploitedj earned 125 total points
ID: 40318315
Have you tried instead:

find / -maxdepth 1 ! -wholename "/proc"
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 40318335
How about

shopt -s extglob
for i in `ls -d /!(proc)`
do
  echo $i
done

To exclude e. g. /proc, /tmp and /sys use

ls -d /!(proc|sys|tmp)
0
 

Author Comment

by:TIMFOX123
ID: 40318395
Here is what I am going to try tomorrow

 for J in `find / -maxdepth 1 -mindepth 1  ! -wholename "/proc" ! -wholename "/dev"`
 do
 du -ks $J
 done

I will try to close this then and thank you both very much
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:TIMFOX123
ID: 40318420
Hello Porky

you know I think you are a real brain and you give great answers.

I tried to run this and it did not seem to run correctly.  I just pasted it in to bash as root  

Could you try running it again pls

shopt -s extglob
 for i in `ls -d /!(proc)`
 do
   echo $i
 done
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
ID: 40318810
Hi again Foxy,

"It works for me".

Could you please try step by step?

shopt -s extglob
shopt extglob


You should see

extglob    on

Now try

ls -d /!(proc|dev)

You should see a multi column list of your top level directories, with the exceptions of "/proc" and "/dev".

If extended globbing doesn't work for you for some strange reason you can always try

ls -d /* | grep -Ev "/proc|/dev"

By the way, your "find" command (with maxdepth coming first) works just fine for me, too!

wmp
0
 

Author Comment

by:TIMFOX123
ID: 40318917
Hello wmp

you were right and I do not know what I did wrong the first time.  

Thank you for you patience.

you always make me a better admin so double thanks
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40318951
You're always welcome!

Seems you want to run "du -ks" against the selected top level directories? No need for a loop then:

shopt -s extglob
du -ks $(ls -d /!(proc|dev))

will do the trick. You can add a "sort" to bring the biggest directories to the bottom/top of the list.

Bottom:
du -ks $(ls -d /!(proc|dev)) | sort -n

Top:
du -ks $(ls -d /!(proc|dev)) | sort -nr

wmp
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 total points
ID: 40319034
No need for ls either
du -ks /!(proc|dev)
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40319064
Yes, I always forget that extglob would work for more things than just "ls"  ...

Thx, ozo!
0
 

Author Closing Comment

by:TIMFOX123
ID: 40320394
Thank you all

great job
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question