I'm trying to use pattern matching to find all files within a directory that have an extension of either .jpg or jpeg.
ls *.[jJ][pP][eE][gG]
<- this obviously will only find the .jpeg file extension. The question is, how do I make the [eE
optional?
Match harder.
ls *.[jJ][pP]{[eE],}[gG]
As well as the standard (simple) glob patterns, bash ≥4.0 has extended globbing.
It is off by default. To turn it on, use: shopt -s extglob
With extglob
you have access to extended regular expression patterns as well as the standard patterns. Furthermore, in your particular situation, you can tailor your glob's behaviour even further by enabling a case insensitive glob, which is also off by default. To turn it on, use: shopt -s nocaseglob
Enabling extglob
does not alter how standard globs work. You can mix the two forms. The only issue is that you have to be aware of the special extended regex syntax. eg, In the example below, the only part of it which is an extended regex, is ?(e)
. The rest is standard glob expansion, with case-insensitivity enabled.
The extended-regex, case-insensitive glob for your situation is:
shopt -s extglob nocaseglob
ls -l *.jp?(e)g
You can find more info and examples at: Bash Extended Globbing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With