10

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 ]
then
echo jpg files found: $count ; jpg="yes"
fi

Considering file extensions jpg, png, bmp, raw and others, should I use a while cycle to do this?

13

I'd suggest a different approach, avoiding the possible word-splitting issues of ls

#!/bin/bash

shopt -s nullglob

for 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 

done

You can loop over the files array with any other commands you want to perform on the files of each particular extension.

  • @1_CR oops thank you - must have brace expansions on the brain! will correct above – steeldriver Jul 26 '14 at 21:37
20

My approach would be:

  1. List all files in the directory
  2. Extract their extension
  3. Sort the result
  4. Count the occurrences of each extension

Sort of like this (last awk call is purely for formatting):

ls -U | awk -F . '{print $NF}' | sort | uniq -c | awk '{print $2,$1}'
  • mhmh... later should I filter each extension found for do an action for it? – watchmansky Jul 26 '14 at 19:24
  • It depends on what you want to do in the end. Can you give more information? – groxxda Jul 26 '14 at 19:25
  • My goal: a script that process each extension file (only image file) changing the size from input user data. So, I start from how many jpg files there're, next png, etc. – watchmansky Jul 26 '14 at 19:27
  • steeldrivers solution may be more appropriate then. – groxxda Jul 26 '14 at 19:30
  • 2
    I had both JPG and jpg files, and wanted it recursively so my solution was to write find . -type f | awk -F . '{print tolower($NF)}' | sort | uniq -c | awk '{print $2,":",$1}' – Kristian May 24 '17 at 12:40
7

This recursively traverses files and counts extensions that match:

$ find . -type f | sed -e 's/.*\.//' | sort | uniq -c | sort -n | grep -Ei '(tiff|bmp|jpeg|jpg|png|gif)$'
   6 tiff
   7 bmp
  26 jpeg
  38 gif
  51 jpg
  54 png
3

Anything involving ls is likely to produce unexpected results with special chars (space and other symbols). Any bashism (like arrays) isn't portable. Anything involving while read is usually slow.

On the other hand, find is VERY flexible (lots of options to filter), it has [at least] two syntax which are fail safe for special chars... and It scales well on large directory.

For this example, I have used -iname to match both upper and lower case extension name. I have also restricted the -maxdepth 1 to respect your question's "in current directory". Rather than counting the number of lines, where filenames could include CR/LF, -print0 will print a NULL byte at the end of each filename... so | tr -d -c "\000" | wc -l is accurately counting files (NULL bytes!).

extensions="jpg png gif"
for ext in $extensions; do
  c=$(find . -maxdepth 1 -iname "*.$ext" -print0 | tr -d -c "\000" | wc -c)
  if [ $c -gt 0 ]; then
    echo "Found $c  *.$ext files"

    find . -maxdepth 1 -iname "*.$ext" -print0 | xargs -0 -r -n1 DOSOMETHINGHERE
    # or #  find . -maxdepth 1 -iname "*.$ext" -exec "ls" "-l" "{}" ";"
  fi
done

P.S. -print0 | tr -d -c "\000" | wc -c can be replaced with -printf "\000" | wc -c or even -printf '\n' | wc -l.

3
find -type f | sed -e 's/.*\.//' | sort | uniq -c
  • 3
    Don't forget a starting directory with find. Also, it can help future readers of these answers if you give a brief explanation of your solution (in case they would like to modify it for a slightly different case). – Jeff Schaller Oct 22 '15 at 16:04
  • How well does this solution deal with path names containing spaces? Newlines? – dhag Oct 22 '15 at 17:37
  • find defaults to the current directory, which is how I use this. I don't think God intended filenames to have spaces in them, but this works fine for that case. If you have newlines, then you deserve all you get. I thought about an explanation but decided it would make the answer too long, I think simplicity is what matters. 99% of the cases in 1% of the time. This is probably Version 7 compatible. – Neik Oct 22 '15 at 21:22
2

Maybe it can get shorter

exts=( *.jpg *.png *.gif ); printf "There are ${#exts[@]}" extensions;
1

can just use ls for something this simple IMO

ls -l /opt/ssl/certs/*.pem | wc -l

or

count=$(ls -l /some/folder/*.jpg | wc -l)

or

ls *.{mp3,exe,mp4} 2>/dev/null | wc -l
  • Why the -R? Are you expecting that some patterns are expanded to folder names? – Kusalananda May 12 '18 at 6:31
  • Good point , was an oversight , I removed it – Mike Q May 12 '18 at 17:13
0

If you are sure of the extension, you can go with find like

find *.jpeg | wc -l
  • until someone creates touch $'foo\nbar.jpeg and it gets counted twice instead of once. Or worse, someone does mkdir directory.jpeg; touch directory.jpeg/{1..100}.txt – Jeff Schaller Jan 31 at 12:16
0

I just wanted to get the total:

find ./ -type f | sed -e 's/.*\.//' |  uniq -c | sort -u | awk '{print $1}' | awk '{total = total + $1}END{print total}'

output:

2347623

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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