bash通配符n位数字 [英] bash wildcard n digits

查看:246
本文介绍了bash通配符n位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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:

 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

我也尝试使用{0..999},但这也会导致相同类型的错误(以及更多错误). 在没有专家错误的情况下如何执行此操作的任何线索?

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?

推荐答案

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

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

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

其中的模式+([0-9])匹配一个或多个数字.

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

您可以在man bash模式匹配部分中详细了解此内容.

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

更新

实际上,正如@chepner指出的那样,由于extglob是在2.02版中引入的,因此,除非您将发行版从石头或其他东西中拿出来,否则它几乎可以在您今天遇到的每个bash中使用.

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

这篇关于bash通配符n位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆