使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
使用find搜索*.js文件时,如何排除特定目录?
find . -name '*.js'
当前回答
我在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' -not \( -path "./dir1" -o -path "./dir2/*" \)
要添加目录,请添加-o-path“./dirname/*”:
find . -name '*.js' -not \( -path "./dir1" -o -path "./dir2/*" -o -path "./dir3/*"\)
但是,如果有许多目录要排除,也许您应该使用正则表达式。
find -name '*.js' -not -path './node_modules/*' -not -path './vendor/*'
似乎与
find -name '*.js' -not \( -path './node_modules/*' -o -path './vendor/*' \)
并且更容易记住IMO。
如果搜索目录有模式(在大多数情况下);您可以简单地如下所示:
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
如果有人想在Makefile中添加find命令,下面是我们排除目录的方法
! -路径“*/directoryName/*”
以下是格式化所有golang文件的示例,不包括protobuf go文件和供应商目录下的所有文件:
find . ! -name '*.pb.go' -name '*.go' ! -path "*/vendor/*" -exec gofmt -s -w '{}' +