How to Find Linux Files With Extensions?

In Linux, searching for specific files is a difficult process, especially when you have a lot of files in the directory or you want to search for more than one file simultaneously. There are some utilities such as “find” and “locate” that can be used in this case. These commands have various features like searching for files with extensions, modification time, file size, etc.

This post will address the possible methods to find files with extensions in Linux. The content for the write-up is as follows:

Method 1: Using find Command

The “find” command is a utility to search the files and directories. It also assists in searching the files with extensions. The syntax for the “find” command is given below.

Syntax:

$ find [Path] [-Options] [Expression]

Type the “find” keyword, “path” where you want to search, “options” such as name, and “expression” to search for any files.  

Let’s practice and search some files using the “find” utility in Linux.

Example 1: Finding Files of Specific Extensions

To find files of a specific extension, the wildcard character “*” is utilized before the extension. This example will illustrate various scenarios to find files by extension. Let’s dig into them:

Finding Text Files

In the following example command, all the text files from the directory “dir1/demo” will be retrieved:

$ find dir1/demo -name "*.txt"

The output shows the name along with its path.

Finding “.sh” Files

Similarly, to display “.sh” extensions files in the directory is obtained as follows:

$ find /home/itslinuxfoss -name "*.sh"

The above image shows all the files with the “.sh” extension.

Example 2: Finding Multiples Files With Extension

Using the “find” command, users can also search the multiple files with their extension name. The syntax for searching the multiple files is shown below:

Syntax:

$ find [Path] \( [-option] [Expression 1] -o [-option] [Expression 2]...[-option] [Expression n] \)

Type the “find” keyword, and “path” where you want to search, type “option”, and “expression” multiple times, and separate them by “o” option. “o” is representing  OR.

Let’s apply this syntax in the terminal for multiple files. In this case, we are searching the “.gz” and “.deb” extension files via the below-mentioned syntax:

$ find dir1/demo \( -name "*.gz" -o -name "*.deb" \)

In the above image, we can see that files with the given extension’s name have been displayed.

Note: The find command will search the files in the folders/directories and the sub-folder/sub-directories.

Let’s move to the second method to find the Linux files with Extensions.

Method 2: Using locate Command

There is another “locate” command available that can be used to search the files with extensions. The Locate command is not pre-installed on some distributions of Linux. Still, it can be installed by installing the “locate” package:

For Debian-Based Distros:

$ sudo apt install plocate

For Fedora-Based Distros:

$ sudo dnf install locate

For Arch-Based Distros:

$ sudo pacman -S locate

The syntax for the locate command is given below:

Syntax:

$ locate [Searching Path] [Expression]

Type the “locate” keyword, “Path” for searching files, and “expressions” for searching files.

Finding “.deb” Extension Files

In this example, “.deb” extension files will be searched. To do this, execute the below command:

$ locate "/home/*.deb"

All the files in the “home” directory and the sub-directories have been displayed, as shown in the above image. 

Finding “.zip” Extension Files

Likewise, for searching any file having “.zip” extensions are obtained as follows:

$ locate "/home/*.zip"

The above has displayed all the “.zip” files in the given path.

Method 3: Find Files Using grep Command

Except for these two methods, you can also find files with extensions using the “grep” command. The “grep” command is used to search and match any file text. We can use a combination of ls and pipe(|) commands to search the files by their extension name.

Command to search all the files that end with the “.gz” extension is as follows:

$ ls | grep ".*.gz$"

In the command, the “ls” command is used to locate the directory of the files which is sent to the “grep” command as an input and the “grep” command will search for the “.gz” extension files. Here, the “$” sign indicates to search the given expression at the end of the file name:

The files having the “.gz” extensions are displayed on the terminal.

Note: You can use extension names without “.” such as grep “.*gz”.However, the recommended method is practiced in the above command.

Conclusion

To find any Linux file with an extension, “find”, “locate”, and “grep” commands are used, which search the files from the given path. These utilities can be exercised to find files having the same extensions and of different extensions as well. This write-up has demonstrated all the possible methods to search any Linux files with their extensions.