在 bash 中,列出两种类型文件的正则表达式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6109700/
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
In bash, what's the regular expression to list two types of files?
提问by galath
A directory contains .zipand .rarfiles, and files of other type.
目录中包含.zip和.rar文件,以及其它类型的文件。
To list only .rarand .zipfiles, there is ls *.zip *.rar
只列出.rar和.zip文件,有ls *.zip *.rar
In bash, how to match both types with one regex?
在 bash 中,如何将两种类型与一个正则表达式匹配?
回答by Johnsyweb
Do you really want a regular expression?
你真的想要一个正则表达式吗?
This uses *("globbing") and {[...]}("brace expansion").
这使用*(" globbing") 和{[...]}("大括号扩展")。
$ ls *.{zip,rar}
See also this questionfor many, many more shortcuts.
另请参阅此问题以了解更多快捷方式。
回答by dogbane
Use brace expansion:
使用大括号扩展:
ls *.{zip,rar}
If you must use a regex, you can use find:
如果您必须使用正则表达式,您可以使用find:
find -regex ".*\.\(zip\|rar\)"
回答by Phil
In bash you can turn on the special extgloboption to do this with a regex:
在 bash 中,您可以打开特殊的extglob选项以使用正则表达式执行此操作:
shopt -s extglob
ls *.*(zip|rar)
The advantage here is that it will list either or both file types, even if one is not present.
这里的优点是它会列出一种或两种文件类型,即使没有。
(As with all shell glob matching, the pattern will be passed directly to ls if there is no match; disable this behaviour with shopt -s failglob)
(与所有 shell glob 匹配一样,如果没有匹配,模式将直接传递给 ls;使用 禁用此行为shopt -s failglob)

