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
 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.  
如果-prune不适合你,这将会: 
find -name "*.js" -not -path "./directory/*"
I find the following easier to reason about than other proposed solutions:
find build -not ( -path build/external -prune ) -name *.js
This comes from an actual use case, where I needed to call yui-compressor on some files generated by wintersmith, but leave out other files that need to be sent as-is.
 Inside ( and ) is an expression that will match exactly build/external , and will, on success, avoid traversing anything below.  This is then grouped as a single expression with the escaped parenthesis, and prefixed with -not which will make find skip anything that was matched by that expression.  
 One might ask if adding -not will not make all other files hidden by -prune reappear, and the answer is no.  The way -prune works is that anything that, once it is reached, the files below that directory are permanently ignored.  
That is also easy to expand to add additional exclusions. For example:
find build -not ( -path build/external -prune ) -not ( -path build/blog -prune ) -name *.js
下一篇: 如何在find中排除目录。 命令
