Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In a bash script, how can I say 'for all files of type .png or .PNG'?

I'm trying :

for i in (`ls *.PNG` && `ls *.png`)

but getting a syntax error.

share|improve this question
add comment

3 Answers

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.

share|improve this answer
    
find is however by default recursive, so to get the same result the question asks, you'd have to do find -maxdepth 1 -iname "*.png" –  Yab May 16 '11 at 6:45
    
@Yab, you are perfectly correct. I overlooked this aspect. –  Alain Pannetier May 16 '11 at 6:51
add comment

ls is almost useless here but for the record here is a syntax closer to your attempt:

for i in $(ls *.PNG ; ls *.png)

Notes:

  • I'm assuming you want both uppercase and lowercase pictures. The && separator would means process png files only if PNG files exist, which probably doesn't make much sense.
  • you'll have error messages displayed if no files are found with either pattern.
share|improve this answer
add comment

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.

share|improve this answer
3  
for i in *.{png,PNG} –  glenn jackman May 16 '11 at 12:54
    
@glenn: That'd work too (equivalent to `*.png *.PNG), although you still need nullglob or the existence check. –  Gordon Davisson May 16 '11 at 20:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.