bash 递归查找匹配特定模式的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23207896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:48:18  来源:igfitidea点击:

Recursively find all files that match a certain pattern

macosbashcommand-line

提问by pw222

I need to find (or more specifically, count) all files that match this pattern:

我需要找到(或更具体地说,计数)与此模式匹配的所有文件:

*/foo/*.doc

*/foo/*.doc

Where the first wildcard asterisk includes a variable number of subdirectories.

其中第一个通配符星号包括可变数量的子目录。

采纳答案by rici

With gnu find you can use regex, which (unlike -name) match the entire path:

使用 gnu find 您可以使用正则表达式,它(与-name)匹配整个路径:

find . -regex '.*/foo/[^/]*.doc'

To just count the number of files:

只计算文件数:

find . -regex '.*/foo/[^/]*.doc' -printf '%i\n' | wc -l

(The %iformat code causes findto print the inode number instead of the filename; unlike the filename, the inode number is guaranteed to not have characters like a newline, so counting is more reliable. Thanks to @tripleee for the suggestion.)

%i格式代码导致find打印 inode 编号而不是文件名;与文件名不同,inode 编号保证没有换行符之类的字符,因此计数更可靠。感谢 @tripleee 的建议。)

I don't know if that will work on OSX, though.

不过,我不知道这是否适用于 OSX。

回答by MonkeyWidget

how about:

怎么样:

find BASE_OF_SEARCH/*/foo -name \*.doc -type f | wc -l

find BASE_OF_SEARCH/*/foo -name \*.doc -type f | wc -l

What this is doing:

这是在做什么:

  • start at directory BASE_OF_SEARCH/
  • look in all directories that have a directory foo
  • look for files named like *.doc
  • count the lines of the result (one per file)
  • 从目录 BASE_OF_SEARCH/ 开始
  • 查看所有具有目录 foo 的目录
  • 查找名为 *.doc 的文件
  • 计算结果的行数(每个文件一行)

The benefit of this method:

这种方法的好处:

  • not recursive nor iterative (no loops)
  • it's easy to read, and if you include it in a script it's fairly easy to decipher (regex sometimes is not).
  • 不递归也不迭代(无循环)
  • 它很容易阅读,如果你将它包含在脚本中,它很容易破译(正则表达式有时不是)。

UPDATE: you want variable depth? ok:

更新:你想要可变深度?好的:

find BASE_OF_SEARCH -name \*.doc -type f | grep foo | wc -l

find BASE_OF_SEARCH -name \*.doc -type f | grep foo | wc -l

  • start at directory BASE_OF_SEARCH
  • look for files named like *.doc
  • only show the lines of this result that include "foo"
  • count the lines of the result (one per file)
  • 从目录 BASE_OF_SEARCH 开始
  • 查找名为 *.doc 的文件
  • 只显示包含“foo”的结果行
  • 计算结果的行数(每个文件一行)

Optionally, you could filter out results that have "foo" in the filename, because this will show those too.

或者,您可以过滤掉文件名中包含“foo”的结果,因为这也会显示这些结果。

回答by Teo Tsisme

Based on the answers on this page on other pages I managed to put together the following, where a search is performed in the current folder and all others under it for all files that have the extension pdf, followed by a filtering for those that contain test_texton their title.

根据其他页面上此页面上的答案,我设法将以下内容放在一起,其中在当前文件夹及其下的所有其他文件夹中搜索所有扩展名为pdf 的文件,然后过滤包含test_text 的文件在他们的头衔上。

find . -name "*.pdf" | grep test_text | wc -l

回答by mpez0

Untested, but try:

未经测试,但请尝试:

find . -type d -name foo -print | while read d; do echo "$d/*.doc" ; done | wc -l

find all the "foo" directories (at varying depths) (this ignores symlinks, if that's part of the problem you can add them); use shell globbing to find all the ".doc" files, then count them.

找到所有“foo”目录(在不同深度)(这会忽略符号链接,如果这是问题的一部分,您可以添加它们);使用 shell globbing 查找所有“.doc”文件,然后计算它们。