How to find all the distinct file extensions in a folder hierarchy (Linux)?

Finding all distinct file extensions in a folder hierarchy is a common task in Linux system administration and file management. This operation requires combining several powerful Linux utilities to recursively traverse directories and extract unique file extensions.

Required Linux Commands

The two essential Linux utility commands for this task are −

  • find − Locates files and directories recursively based on specified criteria

  • sed − Stream editor used for text manipulation, searching, and pattern replacement

Finding Extensions in a Single Directory

For a single directory, you can iterate through files without using the find command. This approach uses shell parameter expansion to extract extensions −

for file in *.*; do printf "%s<br>" "${file##*.}"; done | sort -u

The ${file##*.} syntax removes everything up to and including the last dot, leaving only the extension.

Sample Output

app
c
dmg
doc
docx
epub
go
h
htm
jnlp
jpeg
jpg
json
mp4
o
odt
pdf
png
srt
torrent
txt
webm
xlsx
zip

Finding Extensions in Directory Hierarchy

To find distinct file extensions recursively across an entire directory hierarchy, combine find with sed for pattern extraction −

find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort -u

This command works by −

  • find . -type f − Finds all regular files recursively from current directory

  • sed -e 's/.*\.//' − Removes everything up to and including the last dot

  • sed -e 's/.*\///' − Removes any remaining path separators

  • sort -u − Sorts the results and removes duplicates

Alternative Approaches

Here are additional methods to achieve the same result −

Using awk

find . -type f -name "*.*" | awk -F. '{print $NF}' | sort -u

Using basename and parameter expansion

find . -type f -name "*.*" -exec basename {} \; | sed 's/.*\.//' | sort -u

Key Points

  • The find command with -type f ensures only regular files are processed

  • Regular expressions in sed handle edge cases like files with multiple dots

  • The sort -u combination provides alphabetically sorted unique extensions

  • Files without extensions will not appear in the output

Conclusion

Finding distinct file extensions in Linux directories combines the power of find, sed, and sort commands. The recursive approach using find enables comprehensive analysis of entire directory trees, making it invaluable for system administration and file organization tasks.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements