I have a directory with many image files in different formats. I'd like to see a list of all distinct file endings in that directory, so that I can see what kind of image formats are present. So assuming there would be only png, gif and jpg files, my desired output would look like:


I know how to get a list of all file endings using ls and cut, but I don't know how to make that contain only unique entries. Is there a way to do this using only shell tools, or do I have to write a small python script or similar?

$ ls *.* | nawk -F\. '{print $(NF);}' | sort | uniq

If you want to know how many of each type:

$ ls *.* | nawk -F\. '{print $(NF);}' | sort | uniq -c
  • Great, didn't know about the uniq tool - exactly what I was looking for! – lethal-guitar Apr 16 '13 at 13:49
  • +1 but this is a case where awk is overkill; you could achieve the same with for f in ./*.*; do echo "${f##*.}"; done | sort | uniq -c (also, one less pipe) – evilsoup Jul 13 '13 at 15:11

There is always a way.

function alsoHidden {
    find . -maxdepth 1 -type f -printf '%f\n';
$ alsoHidden | sed -E 's/.*\.(.*)/\1/' | sort -u

$ alsoHidden | tr . / | xargs -I% basename % | sort -u

$ alsoHidden | awk -F. ' !a[$NF] { print $NF; a[$NF]++ }'

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.