bash 如何使用 ls 列出以数字结尾的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21792385/
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
How to use ls to list out files that end in numbers
提问by Classified
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.
我不确定我是否在 bash 中正确使用了正则表达式。我在使用 bash shell 的 Centos 系统上。在我们的日志目录中,有一些附加了数字的日志文件,即
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
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:
我不知道正则表达式在 bash 中是如何工作的和/或什么命令(可能是 grep 除外)将输出通过管道传输到。我想到的 cmd/regex 是这样的:
ls -v stream.log(\.\d{0,2})+
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.
毫不奇怪,这行不通。也许我的逻辑是不正确的,但我想从名为 stream.log 的 cmdline 列表文件中说出来,末尾有一个可选的 .xyz,其中 xyz={1..999} 附加在末尾。请让我知道这是否可行,或者我想出的解决方案是否是执行此类操作的唯一方法。在此先感谢您的帮助。
EDIT: Thanks for everyone's prompt comments and reply. I just wanted to bring up that there's also a file called stream.log
that doesn't any digits appended to it that also needs to make it into my ls
listing. I tried the tips in the comment and answer and they work but it leaves out that file.
编辑:感谢大家的及时评论和回复。我只是想提出还有一个名为的文件stream.log
,它没有附加任何数字,也需要将其添加到我的ls
列表中。我尝试了评论和答案中的提示,它们有效,但它遗漏了该文件。
回答by Reinstate Monica Please
You can do this with extended pattern matching in bash, e.g.
您可以使用bash 中的扩展模式匹配来做到这一点,例如
> shopt -s extglob
> ls *'.'+([0-9])
Where
在哪里
+(pattern-list)
Matches one or more occurrences of the given patterns
And other useful syntaxes.
以及其他有用的语法。
?(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
And replace 1000 with some larger number if you have a lot of log files. Though I would prefer the grepoption to this one.
如果您有很多日志文件,请将 1000 替换为更大的数字。虽然我更喜欢grep选项而不是这个选项。
回答by kbshimmyo
An approach using sed
:
一种使用方法sed
:
ls -v stream.log* | sed -nE '/log(\.[0-9]+)?$/p'
and one using egrep
:
一个使用egrep
:
ls -v stream.log* | egrep 'log(\.[0-9]+)?$'
These print out lines that end in "log" and optionally a period and any positive number of digits, followed by the end of the line.
这些打印出以“log”结尾的行以及可选的句点和任何正数的数字,然后是行的结尾。
回答by Kevin
You can this much more simply by just focusing on the dash '-' in the old logfile format. Here is the minimal version:
只需关注旧日志文件格式中的破折号“-”,您就可以更简单地做到这一点。这是最小版本:
ls *-*
This may be a little safer if there are different types of logfiles in the same directory:
如果同一目录中有不同类型的日志文件,这可能会更安全一些:
ls stream.log.*-*
To ensure that you get the one extra file, it does not make sense to generate a confusing regex that will fit it - just include it on the ls line:
为了确保你得到一个额外的文件,生成一个适合它的令人困惑的正则表达式是没有意义的 - 只需将它包含在 ls 行中:
ls stream.log stream.log.*-*
回答by BMW
refer @BroSlow's answer, here is the fix which will include stream.log as well.
参考@BroSlow 的回答,这里是包含 stream.log 的修复程序。
shopt -s extglob
ls stream.log*(.)*([0-9])
stream.log stream.log.1 stream.log.2