使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
当前回答
如果您正在寻找一个高性能的答案,那么它就是:
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有更多的文件,您将获得更高的性能。
其他回答
我在C源文件exclude*.o和exclude*.swp以及exclude(非常规文件)和exclude-dir输出中找到了函数名:
find . \( ! -path "./output/*" \) -a \( -type f \) -a \( ! -name '*.o' \) -a \( ! -name '*.swp' \) | xargs grep -n soc_attach
我更喜欢-不是符号。。。它更可读:
find . -name '*.js' -and -not -path directory
您还可以使用正则表达式包含/排除搜索中的某些文件/目录,具体如下:
find . -regextype posix-egrep -regex ".*\.(js|vue|s?css|php|html|json)$" -and -not -regex ".*/(node_modules|vendor)/.*"
这将只提供所有js、vue、css等文件,但不包括node_modules和vendor文件夹中的所有文件。
我使用find为xgettext提供文件列表,并希望省略特定目录及其内容。我尝试了许多-path与-prune组合的排列,但无法完全排除我想要删除的目录。
虽然我能够忽略我想要忽略的目录内容,但find随后将目录本身作为结果之一返回,结果导致xgettext崩溃(不接受目录,只接受文件)。
我的解决方案是简单地使用grep-v跳过结果中不需要的目录:
find /project/directory -iname '*.php' -or -iname '*.phtml' | grep -iv '/some/directory' | xargs xgettext
我不能肯定,是否有一个论据支持这一发现,它能100%奏效。在头痛之后,使用grep是一个快速而简单的解决方案。
以前的答案在Ubuntu上都不好。试试看:
find . ! -path "*/test/*" -type f -name "*.js" ! -name "*-min-*" ! -name "*console*"
我在这里找到了这个