I'd like list files with the name pattern like [max|min].txt, so execute ls [max|min].txt
in bash shell, but it doesn't work, and the error message I got is: ls: cannot access [max: No such file or directory
so what's the right way to do this job?
Square brackets are for character matching, and vertical bars are for pipes. You're looking for brace expansion .
ls {max,min}.txt
Bash has a shell option called extglob
that you can enable with the command shopt -s extglob
. This will allow you to use the pattern format @(pattern-list)
where pattern-list
is a pipe separated list of patterns. It will match against filenames and will exclude any pattern that does not match a filename, just like the [abc]
range expression. Bash also has brace expansion, but this does not appear to be what you are asking for, as brace expansion does not match against filenames or expand like wildcards or range expressions do.
$ shopt -s extglob
$ touch max.txt min.txt
$ echo @(max|min).txt
max.txt min.txt
$ echo @(min|mid|max).txt
max.txt min.txt
$ echo {min,mid,max}.txt
min.txt mid.txt max.txt
A couple of things to note about the sequence of commands above:
echo @(mid|min|max).txt
does not output mid.txt
because there is no file that matches. echo @(min|mid|max).txt
re-orders the output to be sorted, in the same manner as a wildcard expansion. echo {min,mid,max}.txt
is brace expansion and outputs all elements in the order given.
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.