System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

find files except certain directories

 
SOLVED
Go to solution
f. halili
Trusted Contributor

find files except certain directories

How can I use find command to look for a file let's say filename.txt but ignore certain directories ( /dir1, /dir2 ) during the search?

If there is a better way than find, then let me know please.

Thanks,
f. halili
derekh
4 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: find files except certain directories

Hi:

By example, do something like this:

# find / -xdev -type f -name "*vg*" ! -path "*lvmconf*"

This uses the '-path' argument to skip reporting the contents of the '/etc/lvmconf' directory's files. You can compare the results with and without '! -path "*lvmconf*" to see.

Regards!

...JRF...

Re: find files except certain directories

>JRF: find / -xdev -type f -name "*vg*" ! -path "*lvmconf*"

The problem with "! -path" is that it actually stats each file under that path, then excludes it. The optimal option is -prune except there are no good examples on how to use it.
find / -xdev -type f \( -name "*lvmconf*" -prune -o -name "*vg*" \)

Or to find all files except under SUB_E:
find . -xdev \( -name SUB_E -prune -o -type f \) -type f
Mark McDonald_2
Trusted Contributor

Re: find files except certain directories

The great thing about unix - many way to achieve something.

I would have used the find, then grep'd out the paths I wanted to exclude.
f. halili
Trusted Contributor

Re: find files except certain directories

THANKS
derekh