Bash shell: list all files of type .png or .PNG?

Solution 1:

If you want all possible combinations, use:

for i in *.[Pp][Nn][Gg]; do

or

shopt -s nocaseglob
for i in *.png; do

although that one will make all of your script's shell globs (i.e. wildcard file matches) case insensitive until you run shopt -u nocaseglob.

If you really want just .PNG and .png (and not, for example, .PnG or .pnG), then use either

shopt -s nullglob
for i in *.png *.PNG; do

or

for i in *.png *.PNG; do
    [[ -e "$i" ]] || continue

...the reason for the nullglob or existence check is that if you have only lowercase or only uppercase extensions, it'll include the unmatched pattern in the list of files, leading to an error in the body of the loop. As with nocaseglob, you might want to turn the nullglob shell option off afterward (although in my experience having nullglob on is often good, especially in a script). Actually, I rather consider it a good idea to use either the nocaseglob or existence check for all file matches like this, just in case there are no matches.

Solution 2:

You could also try some one-liner such as

find . -iname "*.png" -exec ....

or

find . -iname "*.png" | xargs ....

Edit
See also @Yab's comment below about recursion.