如何使用ls列出以数字结尾的文件

Cla*_*ied 1 regex linux bash shell ls

我不确定我是否正确使用bash中的正则表达式.我在使用bash shell的Centos系统上.在我们的日志目录中,有附加数字的日志文件,即

stream.log
stream.log.1
stream.log.2 
...
stream.log.nnn  
Run Code Online (Sandbox Code Playgroud)

不幸的是,还有一些带有新命名约定的日志文件,

stream.log.2014-02-14 
stream.log.2014-02-13
Run Code Online (Sandbox Code Playgroud)

我需要获取具有旧日志文件命名格式的文件.我找到了一些有用的东西,但我想知道是否有另一种更优雅的方式来做到这一点.

ls -v stream.log* | grep -v 2014
Run Code Online (Sandbox Code Playgroud)

我不知道正则表达式如何在bash和/或什么命令(除了可能的grep之外)管道输出.我想到的cmd /正则表达式是这样的:

ls -v stream.log(\.\d{0,2})+
Run Code Online (Sandbox Code Playgroud)

毫不奇怪,这不起作用.也许我的逻辑是不正确的但我想从cmdline列表文件中说出名称为stream.log的文件,其末尾附加了一个xyz = {1..999}的可选.xyz.如果这是可行的,或者我想出的解决方案是做这样的事情的唯一方法,请告诉我.在此先感谢您的帮助.

编辑:感谢大家的迅速评论和回复.我只是想提一下,还有一个名为的文件stream.log没有附加任何数字,也需要进入我的ls列表.我尝试了评论和答案中的提示并且它们有效,但它遗漏了该文件.

Rei*_*ase 7

您可以使用扩展模式匹配来执行此操作,例如

> shopt -s extglob
> ls *'.'+([0-9])
Run Code Online (Sandbox Code Playgroud)

哪里

+(pattern-list)
     Matches one or more occurrences of the given patterns
Run Code Online (Sandbox Code Playgroud)

和其他有用的语法.

?(pattern-list)
     Matches zero or one occurrence of the given patterns
*(pattern-list)
     Matches zero or more occurrences of the given patterns
@(pattern-list)
     Matches one of the given patterns
!(pattern-list)
     Matches anything except one of the given patterns
Run Code Online (Sandbox Code Playgroud)

或者,没有扩展模式匹配可以使用不太简洁的解决方案

ls *'.'{1..1000} 2>dev/null
Run Code Online (Sandbox Code Playgroud)

如果你有很多日志文件,请用一些更大的数字替换1000.虽然我更喜欢这个选项.