Sudar
Sudar

Reputation: 19992

Extract the complete list of file extensions from a directory

I have a directory with lot of sub directories in multiple level and I want to extract the unique list of file extensions that are present in all these sub directories.

Is there a simple way to do it from command line?

Thanks.

Upvotes: 0

Views: 577

Answers (2)

fedorqui
fedorqui

Reputation: 289565

You can for example do:

find /your/dir -type f | awk -F. '{a[$NF]} END{for (i in a) print i}'
  • find /your/dir -type f finds files
  • awk -F. '{a[$NF]} END{for (i in a) print i}' gets all the extensions and prints them.

Upvotes: 1

tripleee
tripleee

Reputation: 189367

If your definition of "extension" is "anything after the last dot, or else nothing" then maybe something like

find dir -name '*.*' -print | rev | cut -d . -f1 | rev

Pipe into | sort -u to get just the unique extensions.

This is not robust against file names with newlines in them, etc.

The concept of a "file extension" is not well formalized on Unix, but if you are traversing e.g. a web server's files (which often do depend on an extension scheme) this should work.

Upvotes: 3

Related Questions