List all unique extensions for files contained in a directory

Try this:

find . -type f | sed -rn 's|.*/[^/]+\.([^/.]+)$|\1|p' | sort -u

It outputs nothing for:

  • Files with no extension
  • Files with names that end in a dot
  • Hidden files

It also might be useful to pipe it to sort | uniq -c.


find . -type f | sed -E 's/.+[\./]([^/\.]+)/\1/' | sort -u

Works on OS X, except for files without extension. My downloads folder:

DS_Store
dmg
exe
localized
msi
nib
plist
pmproj
rar
tgz
txt
webloc
zip

You might need sed -r instead?


Minor issue: Files without extensions print their name. Hidden files (such as .DS_Store) print their name without leading ..


Here's yet another solution that does not get confused by file names containing embedded newlines and uses sort -uz to correctly sort file extensions that may have embedded newlines as well:

# [^.]: exclude dotfiles
find . -type f -name "[^.]*.*" -exec bash -c '
   printf "%s\000" "${@##*.}" # get the extensions and nul-terminate each of them
' argv0 '{}' + |
sort -uz | 
tr '\0' '\n' | 
nl

Tags:

Unix