Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

up vote 5 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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