These solutions work for bash. I am not sure if you want the number of each file type or the total.
If you want the total number of image files, try this:
ls {*jpg,*png,*gif} | wc -l
This means ls
anything ending in jpg
, png
or gif
and pipe through wc
.
If you want the number of each, do:
for n in jpg png gif; do echo -ne "$n\t"; ls *$n | wc -l; done
This is a for loop. It will be run 3 times, once for each of jpg png and gif. Each time the loop is run, the $n
variable will take one of the extensions as a value. So, for the first run, ls *$n
will be expanded to ls *jpg
. echo
essentially just means "print". echo -ne
means print without a new line (-n
) and allowing escape characters -e
, this lets me use the tab character \t
.
This will work fine as long as you have at least one file of each type, otherwise it will complain (it will still work, it will just complain). For a slightly more robust version, try this:
for n in jpg png gif; do echo -ne "$n\t"; echo `ls *$n 2>/dev/null | wc -l ` || echo 0; done
This loop is similar to the above but checks if the ls
command returns an error. The ||
operand in bash means "Do this or, if this did not work, do that". So, I am telling bash to ls *jpg
etc and if it does not work, i.e. if there are no files with that extension, echo (print) 0
. The 2>/dev/null
causes any error messages to be discarded.
You can also use awk (this is a slight modification of mauro stettler's answer so it will count only the files with the extensions you are interested in):
ls {*.jpg,*.png,*.gif} | awk -F'.' '{print $NF}' | sort | unic -c
1The OP only wants jpg, png, and gif. Also, you are assuming that there is only one
.
in the file name and no spaces. If you really want to use gawk , print$NF
, not$2
. – terdon – 2013-02-21T11:22:17.867thx,
$NF
was what i was looking for – replay – 2013-02-21T11:23:35.543@mauro.stettler not bad but that will include files with no extension. Also not sure what the -n is for at the end of the second sort. – dublintech – 2013-02-21T11:27:14.120
1the
-n
is to interpret numbers as numbers, and not as strings. otherwise a12
is regarded lower than a2
. – replay – 2013-02-21T11:29:07.830@mauro.stettler ah right I am using gawk on windows. sort - n doesn't work. – dublintech – 2013-02-21T11:30:03.040
about files with no extension, you could modify like this
ls | grep '\.' | awk -F '.' '{print $NF}' | sort | uniq -c | sort -n
– replay – 2013-02-21T11:32:01.947