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

find . -name '*.js'

当前回答

使用多模式-o-name时的另一个示例

在根目录/中搜索所有*.tpl、*.tf文件,不包括位于/src/.traform/和/code/中的文件。

$ find / -type f \( -name '*.tf' -o -name '*.tpl' \) \
  -and \( -not -path '/src/.terraform/*' -and -not -path '/code/*' \)


/src/debug.tf
/src/nodegroup-infra.tpl
/src/variables.tf.tpl

我用hyperfine测试了上述命令;该测试是在具有3k个目录和12k个文件的系统上进行的。我认为可以公平地说,它足够快~70ms

Benchmark #1: ./entrypoint.sh
  Time (mean ± σ):      69.2 ms ±   1.4 ms    [User: 22.6 ms, System: 43.6 ms]
  Range (min … max):    66.4 ms …  72.2 ms    42 runs

目录结构示例

/代码/目录树

bash-5.0# tree /code
/code
├── debug.tf
├── entrypoint.sh
├── nodegroup-infra.tpl
├── tftemplate.sh
└── variables.tf.tpl

0 directories, 5 files

/src/目录树

bash-5.0# tree /src
/src
├── Dockerfile
├── debug.tf
├── entrypoint.sh
├── nodegroup-infra.tpl
├── terraform.tfstate
├── terraform.tfstate.backup
└── variables.tf.tpl

0 directories, 7 files

/根目录树摘要

$ tree /
...
3382 directories, 12164 files

其他回答

一个选项是使用grep排除包含目录名的所有结果。例如:

find . -name '*.js' | grep -v excludeddir

使用多模式-o-name时的另一个示例

在根目录/中搜索所有*.tpl、*.tf文件,不包括位于/src/.traform/和/code/中的文件。

$ find / -type f \( -name '*.tf' -o -name '*.tpl' \) \
  -and \( -not -path '/src/.terraform/*' -and -not -path '/code/*' \)


/src/debug.tf
/src/nodegroup-infra.tpl
/src/variables.tf.tpl

我用hyperfine测试了上述命令;该测试是在具有3k个目录和12k个文件的系统上进行的。我认为可以公平地说,它足够快~70ms

Benchmark #1: ./entrypoint.sh
  Time (mean ± σ):      69.2 ms ±   1.4 ms    [User: 22.6 ms, System: 43.6 ms]
  Range (min … max):    66.4 ms …  72.2 ms    42 runs

目录结构示例

/代码/目录树

bash-5.0# tree /code
/code
├── debug.tf
├── entrypoint.sh
├── nodegroup-infra.tpl
├── tftemplate.sh
└── variables.tf.tpl

0 directories, 5 files

/src/目录树

bash-5.0# tree /src
/src
├── Dockerfile
├── debug.tf
├── entrypoint.sh
├── nodegroup-infra.tpl
├── terraform.tfstate
├── terraform.tfstate.backup
└── variables.tf.tpl

0 directories, 7 files

/根目录树摘要

$ tree /
...
3382 directories, 12164 files

您也可以使用

find  -type f -not -name .directoryname -printf "%f\n"

有很多好的答案,我只是花了一些时间来理解命令的每个元素是什么以及背后的逻辑。

find . -path ./misc -prune -o -name '*.txt' -print

find将开始查找当前目录中的文件和目录,因此查找。。

-o选项代表逻辑OR,并将命令的两部分分开:

[ -path ./misc -prune ] OR [ -name '*.txt' -print ]

不是的任何目录或文件/misc目录不会通过第一个测试路径/其他。但他们将根据第二个表达式进行测试。如果它们的名称与模式*.txt相对应,则会因为-print选项而被打印。

当find到达时/misc目录,此目录仅满足第一个表达式。因此,将对其应用-prune选项。它告诉find命令不要浏览该目录。中的任何文件或目录/find甚至不会探索misc,不会针对表达式的第二部分进行测试,也不会打印。

避免打印修剪后的目录的一个好技巧是在-或-修剪后的右侧使用-print(也适用于-exec)。例如

find . -path "*/.*" -prune -or -iname "*.j2"

将打印当前目录下所有文件的路径,扩展名为“.j2”,跳过所有隐藏目录。Neat。但它也将打印每个正在跳过的目录的完整路径,如上所述。然而,以下内容没有。。。

find . -path "*/.*" -prune -or -iname "*.j2" -print

因为逻辑上有一个隐藏的,在iname运算符之后,在print之前。由于操作的布尔顺序和关联性,这将其绑定到-or子句的右部分。但医生说,如果没有指定它(或它的任何表亲…-print0等),就会有一个隐藏的打印。那么,为什么不是打印的左边部分呢?显然(而且我从第一次阅读手册页时就不明白这一点),如果没有-print或-exec ANYWHERE,那么这是正确的,在这种情况下,-print在逻辑上分散开来,使得所有内容都被打印出来。如果在任何子句中都表达了一个打印样式操作,那么所有隐藏的逻辑操作都将消失,您只能得到指定的内容。现在坦率地说,我可能更喜欢相反的方式,但如果只使用描述性运算符,那么查找显然不会起作用,所以我想这是有意义的。如上所述,这也适用于-exec,因此下面为每个具有所需扩展名的文件提供了完整的ls-la列表,但没有列出每个隐藏目录的第一级。。。

find . -path "*/.*" -prune -or -iname "*.j2" -exec ls -la -- {} +

对我(以及本线程中的其他人)来说,find语法很快就变得非常复杂,所以我总是插入括号以确保我知道什么绑定到什么,所以我通常创建一个用于类型能力的宏,并形成所有这样的语句,如。。。

find . \( \( ... description of stuff to avoid ... \) -prune \) -or \
\( ... description of stuff I want to find ... [ -exec or -print] \)

这样把世界分成两部分是很难出错的。我希望这会有所帮助,尽管似乎不太可能有人读到第30个以上的答案并投票支持,但我们可以希望