开发者

In bash, what's the regular expression to list two types of files?

A directory contains .zip and .rar files, and files of other type.

To list only .rar and .zip files, there is ls *.zip *.rar

In bash, how to match both types wi开发者_运维技巧th one regex?


Do you really want a regular expression?

This uses * ("globbing") and {[...]} ("brace expansion").

$ ls *.{zip,rar}

See also this question for many, many more shortcuts.


Use brace expansion:

ls *.{zip,rar}

If you must use a regex, you can use find:

find -regex ".*\.\(zip\|rar\)"


In bash you can turn on the special extglob option to do this with a regex:

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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜