Find All Files with Extension in Linux
Often, we find ourselves stuck when we have to find all files with the same or different extensions. This has most likely happened to various Linux users while using the terminal. It is one thing to search for a single file type or file, but what will you do when you want to find out all files simultaneously? This article comes to the rescue for our readers who have such a dilemma.
We can use various Linux utilities for finding or locating files on a file system, but searching all files or filenames with the same or different extensions can be difficult and require specific patterns or expressions. In the upcoming section of the article, we will understand the working, syntax, and execution of these utilities.
Find command
One of the most powerful file searching tools in the Linux system is the “find command.” It searches the entire directory for files and folders to get matched with the user’s expression and performs actions on these files. File permission, file size, type are some other factors based on finding files on Linux. Find command also be combined with other utilities such as sed or grep. Now, lets’ head towards the practical implication of find command.
Find command syntax:
$ find Directory Options Expression
Finding all files with a single extension:
To find all files with a file extension, write out its path to find a command with the options and expression specifying the extension. In the below-given example, we will find all files with the “.txt” extension.
$ find . -type f -name “*.txt”
“.” in this command denotes that this tool will find all the “.txt” files in the current directory.
Find “.exe” files in the same find command by adding the extension as “*exe.”
$ find . -type f -name “*.exe”
Configuration files are also an essential part of any file system that can be used for multiple purposes. Write out this command for searching configuration files in the current directory.
$ find /etc -type f -name “*.conf”
Finding files with multiple extension:
You can also add more than extension in your find command so that you can find several extension files easily and quickly.
The execution of below given command will retrieve files with extension “.sh” and “.txt”
$ find . -type f ( -name “*.sh” -o -name “*.txt” )
Locate command
The locate command is a faster and better tool as compared with “find.” When a file is initiated, instead of searching it in the file system, locate utilize the database for the searching requirement. This database stores parts and bits of the information related to files and their addresses on your system.
locate command syntax:
Finding a file with a specific extension, such as “.conf,” which is considered in our case, adds the directory path where the process of searching files will occur.
Find configuration files in the present working directory by utilizing the below-given command.
Similarly, you can follow the syntax of locate command for finding all files with any specific extension such as “.txt.”
Conclusion:
This post covers two powerful yet simple utilities for you to find all files with the same or different extensions. We have provided you the fundamental concepts regarding the “find” and “locate” command and shown you how to utilize these two Linux command-line tools to find all files with several extensions.