Telling find command to ignore directories
Hi,
I'm trying to get the find command to ignore certain directories when searching. I looked in the man page and it recommended -wholename and prune but I tried both and they don't seem to work. What I'm trying to do is something like this: find / -iname \*.iso but I don't want to search in certain directories such as /share, etc. (all of them are on the same filesystem so I can't use -xdev). Any ideas? Thanks for your help. |
Quote:
Quote:
Bear in mind that it is the "-v" switch for grep that really saves you here. |
You can specify the directories you do want to search. For example:
find /home /var /tmp -name '*iso' |
Thanks for your replies. It seems that it's best to pipe to grep or use xargs. jay73, the problem with the suggested approach above is if the directory you want to exclude is a subdirectory of a directory that you want to search. In that case (which is my case), you would have to either:
a) use grep b) use xargs c) try to find out how to do it natively with find |
I think the -wholename -prune approach is the most reliable! Let me do an example. Suppose I have a directory called /test containing three files
Code:
> ls -1 -F /test Code:
> find /test Code:
> find /test -wholename pine Code:
> find /test -wholename \*pine\* Code:
> mkdir /test/newdir Code:
> find /test -wholename \*pine\* Now the intersting part: let's introduce the option -prune (literally to cut off branches from a tree)! First note that -prune is an ACTION (like -print) and not a TEST (like -wholename). This means that -prune performs a task, it is not something to evaluate! This task is: if an entry is a directory do not descend into it. Let's try Code:
find /test -wholename /test/newdir -prune The utility of -prune is clear, if we add another expression by means of the OPERATOR expr1 -o expr2 Code:
> find /test -wholename /test/newdir -prune -o -print Code:
> find /test -wholename /test/newdir -prune -o -name apple -print Code:
find /test \( -wholename /test/newdir -o -wholename /test/pippo \) -prune -o -name apple -print Code:
find / \( -wholename /share -o -wholename /lib -o -wholename /bin \) -prune -o -iname \*.iso -print |
If you use grep to filter the output, the directories like /usr will still be searched which may take some time.
It would be better having an idea on which part of the filesystem the file exists in. The command "find /home/ /tmp -iname "*.iso" would be better then "find / "*.iso". If you install the source for the find command, you can use the "make pdf" target to generate the "Finding Files" document from the texinfo source. It is 60 pages long. |
Thank you very very much colucix for your detailed example :) Thank you also jschiwal for your comment.
|
type is ignored
I used a command somewhat like this to filter out some huge directories.
find . -type d \( -name '*core*' \) -prune -o -print All directories with *core* in the name were pruned as I desired but the final output included files and directories. Why would the -type switch be ignored? |
Quote:
Code:
if(matches) Code:
find / ! -wholename '*/share/*' -name '*.iso' |
On the system I'm using 'find' doesn't have '-wholename' predicate. (apparently older version of binutils). I try to skip directories that start with '.svn', and that worked for me:
Quote:
|
find [source dir] -path [source dir]/[sub dir 1] -prune -o -path [source dir]/[sub dir 2] -prune -o -name [filename]
|
All times are GMT -5. The time now is 08:28 AM. |