helion3 helion3 - 10 months ago 94
Bash Question

How to exclude a directory in find . command

I'm trying to run a find command for all JavaScript files, but how do I exclude a specific directory?

Here is the find code we're using.

for file in $(find . -name '*.js'); do java -jar config/yuicompressor-2.4.2.jar --type js $file -o $file; done

Answer Source

Use the prune switch, for example if you want to exclude the misc directory just add a -path ./misc -prune -o to your find command:

find . -path ./misc -prune -o -name '*.txt' -print

Here is an example with multiple directories:

find . -type d \( -path dir1 -o -path dir2 -o -path dir3 \) -prune -o -print

Here we exclude dir1, dir2 and dir3, since in find expressions it is an action, that acts on the criteria -path dir1 -o -path dir2 -o -path dir3 (if dir1 or dir2 or dir3), ANDed with type -d. Further action is -o print, just print.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download