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

find . -name '*.js'

当前回答

如果搜索目录有模式(在大多数情况下);您可以简单地如下所示:

find ./n* -name "*.tcl" 

在上述示例中;它搜索所有以“n”开头的子目录。

其他回答

使用-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/*"

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

如果搜索目录有模式(在大多数情况下);您可以简单地如下所示:

find ./n* -name "*.tcl" 

在上述示例中;它搜索所有以“n”开头的子目录。

对于我所需要的,它是这样工作的,从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

如果您正在寻找一个高性能的答案,那么它就是:

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有更多的文件,您将获得更高的性能。