bash 通配符 n 位数字

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

bash wildcard n digits

linuxbashwildcard

提问by Rafe

So I've got the following files in the tmp directory:

所以我在 tmp 目录中有以下文件:

 file.0
 file.1
 file.t9
 file.22
 file.4444

if I wanted to list only the files that end in '.digits' (0, 1, 22, 4444) but not (t9) I could try and use wildcards such as this:

如果我只想列出以 '.digits' (0, 1, 22, 4444) 而不是 (t9) 结尾的文件,我可以尝试使用通配符,例如:

 ls tmp/file.{[0-9],[0-9][0-9],[0-9][0-9][0-9],[0-9][0-9][0-9][0-9]}

however I get the following results with the ugly error

但是我得到以下结果与丑陋的错误

 ls: cannot access tmp/file.[0-9][0-9][0-9][0-9]: No such file or directory
 file.0
 file.1
 file.22
 file.4444

I've also tried using {0..999} but that also results in the same sorts of errors (and a lot more of them). Any clues as how to do this without errors from the experts?

我也尝试过使用 {0..999} 但这也会导致相同类型的错误(以及更多错误)。关于如何做到这一点而没有专家错误的任何线索?

回答by janos

At least in bash 4, when the extglobshell option is enabled:

至少在 bash 4 中,当extglob启用 shell 选项时:

shopt -s extglob
ls /tmp/file.+([0-9])

The pattern +([0-9])there matches one or more digits.

+([0-9])那里的模式匹配一位或多位数字。

You can read more about this in the Pattern Matchingsection of man bash.

你可以在阅读更多关于这个模式匹配的部分man bash

UPDATE

更新

Actually, as @chepner pointed out, since extglobwas introduced in version 2.02, this should work in pretty much every bashyou come across today, unless you pulled your distro out of a rock or something.

实际上,正如@chepner 指出的那样,自从extglob在 2.02 版中引入以来,这应该适用于bash您今天遇到的几乎所有内容,除非您将发行版从岩石中提取出来。

回答by paxdiablo

You can use lsjust to list allthe files then filter the output of that through grep:

您可以使用lsjust 来列出所有文件,然后通过grep以下方式过滤输出:

ls -1 | grep -E '\.[0-9]+$'

as per the following test:

根据以下测试:

pax> printf 'file.0\nfile.1\nfile.t9\nfile.22\nfile.4444\n' | grep -E '\.[0-9]+$'
file.0
file.1
file.22
file.4444

The -Egives you extended regular expressions so that the +modifier works. If that's not available to you, you can use the \.[0-9][0-9]*$regex instead.

-E您扩展正则表达式,以便使+修改的作品。如果这对您不可用,您可以改用\.[0-9][0-9]*$正则表达式。

回答by paxdiablo

Another variant for filtering files of a specific extension is to use the findcommand with a set of predicates:

过滤特定扩展名的文件的另一个变体是使用find带有一组谓词的命令:

find tmp/ -type f -iregex '^.*\.[0-9]+$'

The -typepredicate matches only files and the -iregexmatches names that end with one or more digits - ignoring case of the name. If you want to filter files that begin with fileyou would use the following instead:

-type谓词只匹配文件和-iregex匹配的名称为此与一个或多个数字-忽略了名的情况。如果要过滤以 开头的文件,file请改用以下内容:

find tmp/ -type f -iregex '^.*/file\.[0-9]+$'

And finally, if you don't want the whole path displayed for each resulting file, use the following:

最后,如果您不想为每个结果文件显示整个路径,请使用以下命令:

find tmp/ -type f -iregex '^.*/file\.[0-9]+$' -printf "%f\n"

回答by James M.

Another simple solution to just prevent seeing (ie hide) "ugly errors" is to redirect standard error to /dev/nullby appending 2> /dev/null. In your example:

另一个防止看到(即隐藏)“丑陋错误”的简单解决方案是/dev/null通过附加2> /dev/null. 在你的例子中:

ls tmp/file.{[0-9],[0-9][0-9],[0-9][0-9][0-9],[0-9][0-9][0-9][0-9]} 2> /dev/null

This would give you the results you want and hide any errors. Works with any linux command or executable that sends output to stdout and errors to stderr. Note that this does not affect the command return status ($?), it just suppresses errors.

这会给你你想要的结果并隐藏任何错误。适用于任何将输出发送到 stdout 并将错误发送到 stderr 的 linux 命令或可执行文件。请注意,这不会影响命令返回状态 ( $?),它只会抑制错误。