For the purpose of testing, I'd like count how many images files are inside a directory, separating each image file type by file extension (jpg=yes. This because later it will be useful for another script that will execute an action on each file extension). Can I use something like the following for only JPEG files?jpg=count=`ls -1 *.jpg 2>/dev/null | wc -l`if [ $count != 0 ]thenecho jpg files found: $count ; jpg=yesfiConsidering file extensions jpg, png, bmp, raw and others, should I use a while cycle to do this?
Count files in a directory by extension
bash;shell script;files;wildcards
I'd suggest a different approach, avoiding the possible word-splitting issues of ls#!/bin/bashshopt -s nullglobfor ext in jpg png gif; do   files=( *.$ext )  printf 'number of %s files: %d\n' $ext ${#files[@]}  # now we can loop over all the files having the current extension  for f in ${files[@]}; do    # anything else you like with these files  done doneYou can loop over the files array with any other commands you want to perform on the files of each particular extension.