I want to list the extensions of filenames in a directory, sorted and unique.
First list all the files, leaving out directories using:
find -type f .
Then use awk to get the last three characters of each line, i.e. filename.
find . -type f | awk '{ print substr( $0, length($0) - 3, 4) }'
AWK's substr
function extracts four characters (last argument) of string $0 (first argument) starting at full lenth of the string minus three characters (second argument).
This will list the extensions from all files in the current directory and all directories below.
Now sort
and uniq
the output of the previous command for a short list all of different extensions used.
find . -type f | awk '{ print substr( $0, length($0) - 3, 4) }' | sort | uniq
Bear in mind that sort
has to come before uniq
.
Lastly I'll translate to lower case all the extensions, before sort and uniq, to make the filtering case insensitive:
find . -type f | awk '{ print substr( $0, length($0) - 3, 4) }' | tr '[:upper:]' '[:lower:]' | sort | uniq
Final output of the command is
.avi
.idx
.jpg
.m4v
.mkv
.mp4
.nfo
.png
.smi
.srt
.sub
.txt
.url