Solved

Problem with find command in linux

Posted on 2014-09-11
10
644 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

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

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

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 …
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

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