使用find搜索*.js文件时,如何排除特定目录?

find . -name '*.js'

当前回答

对于我所需要的,它是这样工作的,从root开始在所有服务器中查找landscape.jpg,不包括/var目录中的搜索:

find/-maxdepth 1-type d | grep-v/var | xargs-I“{}”find“{}”-name landscape.jpg

find/-maxdepth 1-type d列出/

grep-v/var从列表中排除“/var”

xargs-I“{}”find“{}”-name landscape.jpg执行任何命令,如find with each directory/result from list

其他回答

如果-prune对你不起作用,这将:

find -name "*.js" -not -path "./directory/*"

注意:需要遍历所有不需要的目录。

find -name '*.js' -not -path './node_modules/*' -not -path './vendor/*'

似乎与

find -name '*.js' -not \( -path './node_modules/*' -o -path './vendor/*' \)

并且更容易记住IMO。

我更喜欢-不是符号。。。它更可读:

find . -name '*.js' -and -not -path directory

最好使用exec操作而不是for循环:

find . -path "./dirtoexclude" -prune \
    -o -exec java -jar config/yuicompressor-2.4.2.jar --type js '{}' -o '{}' \;

执行者…'{}' ... '{}' \; 将对每个匹配的文件执行一次,将大括号“{}”替换为当前文件名。

请注意,大括号括在单引号中,以防止它们被解释为shell脚本标点符号*。


笔记

*从find(GNU findutils)4.4.2手册页的EXAMPLES部分

对于FreeBSD用户:

 find . -name '*.js' -not -path '*exclude/this/dir*'