Being a Linux user, you might face a situation where you require to search for multiple files having different extensions. However, searching for files in Linux, especially when looking for multiple files simultaneously in a directory containing many files, can be a tricky process.
But don’t worry, if you’re looking for some easy methods to perform this task then you’ve surely come to the right place!!!
In this guide, we will cover different methods or utilities of Linux that will help you to find desired files in your directory with different extensions.
1. Search Files With Specific Extensions in Linux
The find command is a beneficial tool employed to search/locate files and directories in Linux, which also provides the facility of searching the files by their extensions.
Let’s state the syntax of the find command to search for all the files that have any specific extension.
$ find . -type f -name "*.<fileExtension>"
Here is the breakdown of the above syntax:
-type– This flag indicates the type of documents to search, with
"f"defining the type as “file“.
-name– Its purpose is to find files by their names. In our case, there are extensions, so the
-nameflag limits the search to the specific extension mentioned after it.
“*”represents all the files that have defined extensions.
Now that we have discussed the syntax of the find command to search files depending on their extensions, let’s see some examples to understand it’s working better.
Find Configuration Files in Linux
We will find all the files which have the extension
“.conf” by executing the command listed below.
$ find . -type f -name "*.conf"
As you observed, the command successfully listed all the files with the
“.conf” extension (configuration files).
Find Zip Files in Linux
Moving forwards, let’s try to find all the files with the
“.zip” extension in the “Documents” directory by executing the stated command.
$ find ~/Documents -type f -name "*.zip"
Find Multiple Files (Extensions) in Linux
You can simultaneously search for files with different extensions by using the “find” command with the
"-o" flag. For example, the command mentioned below to locate all the files with the
$ find ~/ \( -name "*.xconf" -o -name "*.zip" \)
2. Find Files by Extensions Using Find and Grep Commands
Now that you’ve understood how to find files using the find command, let’s take a look at its combination with the grep command, which is a very useful command offered by Linux to find text patterns within the searched files. The files are searched by the find command and then text patterns are identified by the grep filter.
The syntax of the find command with the grep command to locate files by extensions is stated below.
$ find . -type f | grep
Let’s understand the above syntax:
|– It is utilized to combine two commands, in our case: find and grep. It gets output from the find command i.e.; all the files in the directory and gives them as input to the grep filter, which will filter the files with a specific extension and list them.
grep– It is the keyword for the grep command and it filters the files with a specific extension.
<fileExtension>– The file extension is the type of file that you’re supposed to look for.
Find PNG Files in Linux
Let’s execute the command mentioned below to find the files in the directory “Downloads” and the grep command will filter the files with the
“.png” extension only.
$ find ~/Downloads -type f | grep .png
3. Find Files by Extensions Using Grep and ls Commands
If you are already aware of the directory where your files exist, navigate to it, and then use the ls command to search files by extension from the current directory. However, we can further combine it with the grep command to obtain our desired result.
$ ls | grep -i ".<fileExtension>"
Let’s analyze the provided syntax:
ls– This command displays the list of the files and directories in the current working directory.
|– It will join the ls command with the grep command, to provide all the files in the current directory to the grep command to search for files by extension.
-i– This flag enables grep to search for a specific text pattern in the files irrespective of the case of the letters.
Let’s execute the command mentioned below to locate all the files extension “.conf” in the current directory, irrespective of the letter case.
$ ls | grep -i ".conf"
Now that you have used the “find” and “ls” commands, let’s discuss another command for searching files by extensions in Linux.
4. Find Files by Extensions Using locate Command
The “locate” command is a potent utility used to find files and directories in most Linux systems using the pre-built database of filenames and paths, which makes it convenient and easy to use.
$ locate "<fileName>"
It comes pre-installed in most Linux systems, however, if you don’t have one, you can install it using your package manager as shown.
$ sudo apt install mlocate [On Debian, Ubuntu and Mint] $ sudo yum install mlocate [On RHEL/CentOS/Fedora and Rocky/AlmaLinux] $ sudo emerge -a sys-apps/mlocate [On Gentoo Linux] $ sudo apk add mlocate [On Alpine Linux] $ sudo pacman -S mlocate [On Arch Linux] $ sudo zypper install mlocate [On OpenSUSE]
Once installed, run the following command to update the file database used by the locate command on your system.
$ sudo updatedb
Once the updatedb process is complete, you can use the locate command to quickly find files and directories on your system based on their names or extensions as shown.
$ locate ".xconf"
5. Find Files by Extensions Using GUI Tools
After going through all the command-line commands let’s take a look at the GUI method for finding files. Some people prefer the GUI method over CLI commands.
To find files with desired extensions in any file, follow these steps:
- Open the File Manager.
- Move to any directory in which you want to find the files.
- Activate the search bar and type the desired extension for example
“.zip”as shown in the figure.
We have successfully searched all the files with the extension
File extensions are the suffix that appears after the
“.” (dot) in the file’s name. Sometimes users want to search for files that have a particular extension. This guide provided numerous methods to search files by extensions.