如何使用ls列出以数字结尾的文件 [英] How to use ls to list out files that end in numbers

查看:79
本文介绍了如何使用ls列出以数字结尾的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm not sure if I'm using regular expressions in bash correctly. I'm on a Centos system using bash shell. In our log directory, there are log files with digits appended to them, i.e.

stream.log
stream.log.1
stream.log.2 
...
stream.log.nnn  

不幸的是,还有一些具有新命名约定

Unfortunately there are also log files with the new naming convention,

stream.log.2014-02-14 
stream.log.2014-02-13

我需要获取具有旧日志文件命名格式的文件.我发现了一些可行的方法,但我想知道是否还有另一种更优雅的方法可以做到这一点.

I need to get files with the old log file naming format. I found something that works but I'm wondering if there's another more elegant way to do this.

ls -v stream.log* | grep -v 2014

我不知道正则表达式如何在bash中工作和/或通过什么命令(可能不是grep)将其输出到管道.我在想的cmd/regex是这样的:

I don't know how regular expressions work in bash and/or what command (other than possibly grep) to pipe output to. The cmd/regex I was thinking of is something like this:

ls -v stream.log(\.\d{0,2})+

毫不奇怪,这没有用.也许我的逻辑不正确,但是我想从名称为stream.log的cmdline列表文件中说,结尾处带有可选的.xyz,并在结尾处附加xyz = {1..999}.请让我知道这是否可行,或者我提出的解决方案是否是执行此类操作的唯一方法.预先感谢您的帮助.

Not surprisingly, this didn't work. Maybe my logic is incorrect but I wanted to say from the cmdline list files with the name stream.log with an optional .xyz at the end where xyz={1..999} is appended at the end. Please let me know if this is doable or if the solution I came up with is the only way to do something like this. Thanks in advance for your help.

推荐答案

您可以在,例如

> shopt -s extglob
> ls *'.'+([0-9])

哪里

+(pattern-list)
     Matches one or more occurrences of the given patterns

以及其他有用的语法.

?(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

或者,如果没有扩展的模式匹配,则可以使用一种不太整齐的解决方案

Alternatively without extended pattern matching could use a less neat solution

ls *'.'{1..1000} 2>dev/null

如果日志文件很多,则用更大的数字替换1000.尽管我更喜欢选项的问题.

And replace 1000 with some larger number if you have a lot of log files. Though I would prefer the grep option to this one.

这篇关于如何使用ls列出以数字结尾的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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