使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
当前回答
以前的答案在Ubuntu上都不好。试试看:
find . ! -path "*/test/*" -type f -name "*.js" ! -name "*-min-*" ! -name "*console*"
我在这里找到了这个
其他回答
最好使用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部分
对于那些在旧版本UNIX上无法使用-path或-not的用户
在SunOS 5.10 bash 3.2和SunOS 5.11 bash 4.4上测试
find . -type f -name "*" -o -type d -name "*excluded_directory*" -prune -type f
find . \( -path '.**/.git' -o -path '.**/.hg' \) -prune -o -name '*.js' -print
上面的示例查找当前目录下的所有*.js文件,不包括文件夹.git和.hg,无论这些.git和.h文件夹有多深。
注意:这也适用于:
find . \( -path '.*/.git' -o -path '.*/.hg' \) -prune -o -name '*.js' -print
但我更喜欢**表示法,以便与其他一些工具保持一致,这在这里可能会偏离主题。
使用-prune主键。例如,如果要排除/其他:
find . -path ./misc -prune -o -name '*.txt' -print
要排除多个目录,请在括号中对它们进行“或”运算。
find . -type d \( -path ./dir1 -o -path ./dir2 -o -path ./dir3 \) -prune -o -name '*.txt' -print
而且,要在任何级别排除具有特定名称的目录,请使用-name primary而不是-path。
find . -type d -name node_modules -prune -o -name '*.json' -print
如果-prune对你不起作用,这将:
find -name "*.js" -not -path "./directory/*"
注意:需要遍历所有不需要的目录。