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
apple
pine
pineapple
If I issue the find command I will get four entries
Code:
> find /test
/test
/test/pine
/test/pineapple
/test/apple
Now, let's start from the option
-wholename pattern. Only the entries matching the pattern will be listed. The whole entries!!!
Code:
> find /test -wholename pine
>
Nothing! Indeed, there is no entry that matches exactly
pine. Now, let's add wildcards to the pattern.
Code:
> find /test -wholename \*pine\*
/test/pine
/test/pineapple
Here there are two entries that match
pine with something before and/or something after! Now, let's create a directory inside /test and put duplicates of the same three files inside it. Then issue the find command.
Code:
> mkdir /test/newdir
> touch /test/newdir/pine
> touch /test/newdir/pineapple
> touch /test/newdir/apple
> find /test
/test
/test/pine
/test/newdir
/test/newdir/pine
/test/newdir/pineapple
/test/newdir/apple
/test/pineapple
/test/apple
Now, let's try the find command with the two wildcards again. Then issue the find command using the name of a directory as pattern!
Code:
> find /test -wholename \*pine\*
/test/pine
/test/newdir/pine
/test/newdir/pineapple
/test/pineapple
> find /test -wholename /test/newdir
/test/newdir
The former has retrieved two of the new files, as expected. The latter has retrieved the entry matching exactly /test/newdir.
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
/test/newdir
here the test is true (the entry /test/newdir matches the pattern) but since we have not searched for anything else, the result is not meaningful. Actually the rule is
if the expression contains no actions other than -prune, -print is performed on all files for which the expression is true.
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
/test
/test/pine
/test/pineapple
/test/apple
here we have -prune and -print, so the /test/newdir entry is not printed anymore; the directory /test/newdir is excluded from the search path; any other entry whose wholename does not match exactly /test/newdir is printed. Now add a TEST to the second expression
Code:
> find /test -wholename /test/newdir -prune -o -name apple -print
/test/apple
the trick is done! We have found a file called apple excluding that one inside /test/newdir. If you want to exclude more than one directory you can do
Code:
find /test \( -wholename /test/newdir -o -wholename /test/pippo \) -prune -o -name apple -print
that is in the first expression you will put two TESTS enclosed in escaped ( ) and separated by the OPERATOR -o. Finally, in your example you will have something like
Code:
find / \( -wholename /share -o -wholename /lib -o -wholename /bin \) -prune -o -iname \*.iso -print
I hope these options are a little more clear now, despite my bad english! Sorry for the very long post!
