简体   繁体   中英

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 with 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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM