
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
How to find all the distinct file extensions in a folder hierarchy (Linux)?
While there are plenty of ways to find the extension of a particular file in Linux using different utility commands, if we need to find all the distinct file extensions in a folder hierarchy we need to first understand the uses of the find and the sed command as these commands will be used to print all the distinct file extensions in a folder or a folder hierarchy.
The two Linux utility commands that we must be aware of are −
find − used to locate a particular file or directory
sed − short for stream editor and is used to perform functions like searching, editing and replacing.
When we are talking about a single folder then we don’t even need the find command as we can simply do that by iterating over all the files and then making use of the sort command as well.
Let’s say that I have a directory called dir1, and I want to know the file extensions in this folder that are distinct.
For that I will type the command shown below inside that directory.
Command
for file in *.*; do printf "%s
" "${file##*.}"; done | sort -u
Output
immukul@192 dir1 % for file in *.*; do printf "%s
" "${file##*.}"; done | sort -u app c dmg doc docx epub go h htm jnlp jpeg jpg json mp4 o odt pdf png srt torrent txt webm xlsx zip
As you can notice, all the extensions listed out in the above examples are distinct. Now, if we want to list all the distinct file extensions in a file hierarchy then we need to add a recursion to the above command.
Command
find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort -u
Output
immukul@192 dir1 % find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort -u app c dmg bz2 callgrind case-hosts cc cfg cgi comments conf config contention cov cpu crash crt css csv dat debug_rnglists demangle-expected dep description
- Related Articles
- How to select all distinct filename extensions from a table of filenames in MySQL?
- How to read all files in a folder to a single file using Java?
- How to search a directory with file extensions in Java?
- How to quickly sum all the numbers in a file on Linux?
- Find all links for a specific file on Linux
- How to delete a file from the public folder in Laravel?
- How to filter the files by file extensions and show the file names in Java?
- How can I copy a file from one folder to another folder within a container in Docker?
- How to Find Out File Types in Linux
- How to find the most recent file in a directory on Linux?
- Python - How to Merge all excel files in a folder
- How to Find and Replace Text in a File on Linux
- How to move a file from one folder to another using Python?
- How to sort a file in-place in Linux?
- How to Randomize Lines in a File in Linux
