使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
当前回答
以前的答案在Ubuntu上都不好。试试看:
find . ! -path "*/test/*" -type f -name "*.js" ! -name "*-min-*" ! -name "*console*"
我在这里找到了这个
其他回答
这是因为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
当做
我更喜欢-不是符号。。。它更可读:
find . -name '*.js' -and -not -path directory
如果您正在寻找一个高性能的答案,那么它就是:
find . -type d -name node_modules -prune -false -o -type f
使用-false排除node_modules本身。
在node_modules中包含10000个文件的目录中,它将比非路径方法快3倍。
find . -type f -not -path '*node_modules*'
如果node_modules有更多的文件,您将获得更高的性能。
如果-prune对你不起作用,这将:
find -name "*.js" -not -path "./directory/*"
注意:需要遍历所有不需要的目录。
对于FreeBSD用户:
find . -name '*.js' -not -path '*exclude/this/dir*'