使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
当前回答
这是因为find测试模式“*foo*”的文件:
find ! -path "dir1" ! -path "dir2" -name "*foo*"
但如果不使用模式(find不测试文件),它就不起作用。因此find没有使用它以前评估的“true”和“false”布尔值。不使用上述符号的用例示例:
find ! -path "dir1" ! -path "dir2" -type f
没有找到测试!因此,如果您需要查找没有任何模式匹配的文件,请使用-prune。此外,通过使用prune查找总是更快,因为它确实跳过了该目录,而不是匹配它或更好地不匹配它
find dir -not \( -path "dir1" -prune \) -not \( -path "dir2" -prune \) -type f
or:
find dir -not \( -path "dir1" -o -path "dir2" -prune \) -type f
当做
其他回答
这适合我在Mac上使用:
find . -name *.php -or -path "./vendor" -prune -or -path "./app/cache" -prune
它将排除带有php后缀的搜索名称的供应商和app/cache-dir。
如果-prune对你不起作用,这将:
find -name "*.js" -not -path "./directory/*"
注意:需要遍历所有不需要的目录。
这是唯一一个对我有用的。
find / -name MyFile ! -path '*/Directory/*'
正在搜索“MyFile”,不包括“Directory”。强调星星*。
以前的答案在Ubuntu上都不好。试试看:
find . ! -path "*/test/*" -type f -name "*.js" ! -name "*-min-*" ! -name "*console*"
我在这里找到了这个
如何在sh中使用find的prune选项是Laurence Gonsalves关于prune如何工作的一个很好的答案。
下面是通用解决方案:
find /path/to/search \
-type d \
\( -path /path/to/search/exclude_me \
-o \
-name exclude_me_too_anywhere \
\) \
-prune \
-o \
-type f -name '*\.js' -print
要避免多次键入/path/To/seach/,请将查找包装在pushd中。。popd对。
pushd /path/to/search; \
find . \
-type d \
\( -path ./exclude_me \
-o \
-name exclude_me_too_anywhere \
\) \
-prune \
-o \
-type f -name '*\.js' -print; \
popd