Find the distinct file extensions in a folder

This post provides instructions on how to list all the distinct file extensions when a folder contains many many files on Linux (tested on Ubuntu and CentOS)  and Mac OS.

Open a terminal, and

cd into the folder you would like to list the file extensions

Method 1: issue the following command:

$ find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u

This will help us:

  • Find all files from current folder (including files contained in its sub-folders)
  • Prints extension of files if any
  • Make a unique sorted list (e.g., sort -u is for ordering by Alphabet, -u means unique)

See the  pic below for an example.

Method 2: A variation of the method 1.  Issue the following command

$ find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort | uniq -c | sort -n

This will list with counts for each extension (sort -n is for ordering by file number).

See the pic below for an example.

More flags about sort command can be found here (PDF) and here (PDF).

 

For more commonly used Linux commands, check my other posts at here  and here .

Leave a Reply

Your email address will not be published.