find . -type f | perl -ne 'print $1 if /\.([^.\/]+)$/' | sort -u
find . -type f
finds regular files (-type f
) under the current directory (.
) and subdirectories.
perl -ne 'print $1 if /\.([^.\/]+)$/'
tries to match each line with the regular expression pattern /\.([^.\/]+)$/
, where the pattern roughly means: "a dot, followed by at least one character that is neither dot nor slash, until the end of the string". The part after the dot in parentheses (...)
is the file extension, and the parentheses in Perl regular expressions indicate a capturing group, which means we will be able to extract that part of the matched pattern through a variable. If the /.../
expression is successful, there is a match, and the content of the capture group (the file extension) is in the variable $1
, and we print it. The -ne
flags of Perl make it easy to run Perl commands for each line of input.
Finally, sort -u
sorts the file extensions printed by Perl, and filters out duplicate values.
Originally posted at: http://stackoverflow.com/questions/1842254/how-can-i-find-all-of-the-distinct-file-extensions-in-a-folder-hierarchy