bash 使用 shell globbing 匹配所有嵌套目录下的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4349082/
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-17 23:02:12  来源:igfitidea点击:

Match all files under all nested directories with shell globbing

linuxbashshellshopt

提问by Samer Buna

Is there a way to use shell globbing to identify nested directories?

有没有办法使用 shell globbing 来识别嵌套目录?

so if I have dir/dir1/dir2/dir3/dir4/dir5/.. and I have files under all of them, what is the equivalent globbing pattern to match all files under all directories, similar to - for example - ls -R

因此,如果我有 dir/dir1/dir2/dir3/dir4/dir5/.. 并且我在所有文件下都有文件,那么匹配所有目录下所有文件的等效通配模式是什么,类似于 - 例如 - ls -R

采纳答案by Paused until further notice.

In Bash 4, with shopt -s globstar, and zsh you can use **/*which will include everything except hidden files. You can do shopt -s dotglobin Bash 4 or setopt dotglobin zsh to cause hidden files to be included.

在 Bash 4 中,shopt -s globstar您可以使用 , 和 zsh 来**/*包含除隐藏文件之外的所有内容。您可以shopt -s dotglob在 Bash 4 或setopt dotglobzsh 中执行以包含隐藏文件。

In ksh, set -o globstarenables it. I don't think there's a way to include dot files implicitly, but I think **/{.[^.],}*works.

在 ksh 中,set -o globstar启用它。我认为没有办法隐式包含点文件,但我认为**/{.[^.],}*有效。

回答by Tobu

Specifically about git (gitignore, gitattributes, and commands that take filenames): if the pattern contains no slash, *wildcards will match deep. If it does contain a slash, git will call fnmatchwith the FNM_PATHNAMEflag, and simple wildcards won't match slashes. **to match deep isn't supported. Maybe this kind of deep matching could be more widely supported with a new FNM_STARSTARflag, and an implementation in glibc, gnulib and other places.

特别是关于 git(gitignore、gitattributes 和采用文件名的命令):如果模式不包含斜杠,*通配符将匹配深。如果它确实包含斜杠,git 将使用FNM_PATHNAME标志调用 fnmatch,并且简单的通配符将不匹配斜杠。**不支持匹配深度。也许这种深度匹配可以通过一个新的FNM_STARSTAR标志得到更广泛的支持,以及在 glibc、gnulib 等地方的实现。

回答by Brent Newey

If you want to act on all the files returned by find, rather than just list them, you can pipe them to xargs:

如果您想对 find 返回的所有文件进行操作,而不是仅仅列出它们,您可以将它们通过管道传输到 xargs:

find <directory> -type f | xargs ls

But this is only for commands that don't have a recursive flag.

但这仅适用于没有递归标志的命令。

回答by kenorb

You may try:

你可以试试:

**/*.*

However it'll ignore hidden files (such as .gitfiles). Sometimes it's a life-saver.

但是它会忽略隐藏文件(例如.git文件)。有时它是救命稻草。

Read more at: What expands to all files in current directory recursively?at SO

阅读更多内容:什么以递归方式扩展到当前目录中的所有文件?在 SO

回答by Fatih Arslan

You can use tree, it will show all folders recursively.

您可以使用树,它将递归显示所有文件夹。

tree <path>

回答by meagar

There is no way to do this with vanilla Bash, however most commands accept a -Ror --recursiveoption to tell them to descend into directories.

使用 vanilla Bash 无法做到这一点,但是大多数命令都接受 a -Ror--recursive选项来告诉它们进入目录。

If you simply want to list all files located anywhere within a directory or its sub-directories, you can use find.

如果您只想列出位于目录或其子目录中任何位置的所有文件,您可以使用find

To recursively find files (-type f) with a given directory:

递归查找-type f给定目录的文件 ( ):

find <directory> -type f