- How to Find Linux Files With Extensions?
- Method 1: Using find Command
- Syntax:
- Example 1: Finding Files of Specific Extensions
- Finding Text Files
- Finding “.sh” Files
- Example 2: Finding Multiples Files With Extension
- Syntax:
- Method 2: Using locate Command
- For Debian-Based Distros:
- For Fedora-Based Distros:
- For Arch-Based Distros:
- Syntax:
- Finding “.deb” Extension Files
- Finding “.zip” Extension Files
- Method 3: Find Files Using grep Command
- Conclusion
- Find All Files with Extension in Linux
- Find command
- Locate command
- Conclusion:
- About the author
- Talha Saif Malik
- How to Find Files with Extension in Linux
- How to use find command to find all files with extensions from list?
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
- Example 1: Finding Files of Specific Extensions
- Example 2: Finding Multiples Files With Extension
- Finding “.deb” Extension Files
- Finding “.zip” Extension Files
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:
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:
For Fedora-Based Distros:
For Arch-Based Distros:
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:
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:
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:
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.
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:
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.
“.” 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.”
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.
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”
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.
About the author
Talha Saif Malik
Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.
How to Find Files with Extension in Linux
As a Linux user, you might need to locate files with specific extensions for various reasons, such as managing configurations, troubleshooting, or scripting. For example, finding .ini files can help you manage settings for multiple applications. You can do it using the find command. In this article, I will show how to locate files with the .ini extension in the /etc/ directory.
sudo find /etc/ -type f -name «*.ini»
The sudo command is used when searching in the /etc/ directory because it may contain files and directories that are only accessible to the. However, If you are searching in the home directory — sudo is not necessary.
In the command mentioned above, /etc/ is the starting directory for the search. The -type f option instructs the find command to look only for regular files, excluding directories and other file types. The -name option specifies the search pattern. In this case, the wildcard pattern is *.ini. The asterisk (*) wildcard is utilized to match any sequence of characters, while .ini specifies the desired file extension at the end.
You can replace ini with any desired extension to find files with different extensions in Linux. If you are interested in learning more about file searching in Linux, consider reading this article.
Found a mistake in the text? Let me know about that. Highlight the text with the mistake and press Ctrl+Enter.
How to use find command to find all files with extensions from list?
@Gerald: It’s likely you need to group your OR expression in escaped parentheses: find /path/to/ \( -iname ‘*.gif’ -o -iname ‘*.jpg’ \) -exec ls -l <> \; otherwise the exec only applies to the last part ( -iname ‘*.jpg’ in this case).
That’s an important remark. find /path/to/ -iname ‘*.gif’ -o -iname ‘*.jpg’ -print0 will only print the jpg files! You need brackets here: find /path/to/ \( -iname ‘*.gif’ -o -iname ‘*.jpg’ \) -print0
find -E /path/to -regex «.*\.(jpg|gif|png|jpeg)» > log
The -E saves you from having to escape the parens and pipes in your regex.
Hmmm the -E option tells find to use «extended regular expressions». Several other tools have a similar option, but I’m not sure this option is available on all UNIX distributions.
@tboyce12 Working on ubuntu, I can specify the -regextype to simplify the regex expression: find . -regextype posix-extended -regex «.*\.(jpg|gif|png|jpeg)» .
@cjm Maybe find -E /path/to -iregex «.*\.(jpg|gif|png|jpeg)» > log . Using the -iregex flag tells find to match case insensitively.
find /path/to/ -type f -print0 | xargs -0 file | grep -i image
This uses the file command to try to recognize the type of file, regardless of filename (or extension).
If /path/to or a filename contains the string image , then the above may return bogus hits. In that case, I’d suggest
cd /path/to find . -type f -print0 | xargs -0 file --mime-type | grep -i image/