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

find . -name '*.js'

当前回答

如果-prune对你不起作用,这将:

find -name "*.js" -not -path "./directory/*"

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

其他回答

如何在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

如果有人想在Makefile中添加find命令,下面是我们排除目录的方法

! -路径“*/directoryName/*”

以下是格式化所有golang文件的示例,不包括protobuf go文件和供应商目录下的所有文件:

find . ! -name '*.pb.go' -name '*.go' ! -path "*/vendor/*" -exec gofmt -s -w '{}' +

使用-prune选项。因此,类似于:

find . -type d -name proc -prune -o -name '*.js'

“-typed-name-proc-prune”只查找要排除的名为proc的目录。“-o”是“OR”运算符。

对于那些在旧版本UNIX上无法使用-path或-not的用户

在SunOS 5.10 bash 3.2和SunOS 5.11 bash 4.4上测试

find . -type f -name "*" -o -type d -name "*excluded_directory*" -prune -type f

这是唯一一个对我有用的。

find / -name MyFile ! -path '*/Directory/*'

正在搜索“MyFile”,不包括“Directory”。强调星星*。