Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

png
gif
jpg

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?

share|improve this question
up vote 6 down vote accepted
$ 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
share|improve this answer
    
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]++ }'
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .