Find Files by Name in Linux

Most often, you are looking for a file on Linux and you do not exactly know its true location on the system disk.

There are multiple ways to find files in the Linux command line. Most common and most reliable way is to use the find command.

The find command is extremely versatile and has way too many usages but here I'll focus on finding files by their name.

I'll explain how to use the ‘find’ command for:

  • Searching files using their name
  • Searching files with their exact name
  • Searching files for a particular pattern
  • Searching multiple files
  • Excluding certain files from the search results.

Besides these, I'll also show how to use the grep command with the output from the find command. Let’s first start with an overview of the find command.

The utility ‘find’ looks for files that match a certain set of parameters like the file’s name, its modification date, its extension, etc. It has the following format:

find path pattern

If the file path is not specified, it searches in the current directory and its sub-directories.

Searching for Files Using their Name

Looking for a file with its name is a commonly used operation with the find command. The -iname option looks for a file regardless of its case.

For example, suppose you have two files abc.txt and ABC.txt. Both of them have the same name but different cases. Using the find command, you get both files in the results:

find -iname abc.txt
Find files with their name while ignoring the case

Searching for Files Using their Exact Name

The -name option is case-sensitive in contrast to the -iname option, so you are going to get files with the exact name.

For example, let us look for a file with the name abc.txt :

find -name abc.txt

The name of the file can be composed of wildcards as you will see later in this guide.

Searching for Files With a Particular Pattern

You can also filter files that follow a given pattern. For that, you can use wildcards.

Say, for instance, you are looking for all the configuration files on your system that end with the '.conf' extension:

find /etc -type f -name "*.conf" | grep client.conf
Find files with a certain extension

In the same way, you can also search for files with the same name but with any extension of three characters as:

find ~ -name "abc.???" 
Find files with the same name but any extension

If you have several file names that contain a common string, say ‘VM’, the find command in this scenario will be as:

find -name '*VM*' 
Find files with a matching pattern

So far, we have used a single directory (the home directory) with the ‘find’ command.

You can also search in multiple directories by specifying them on the CLI:

find ~/Desktop/example1/ ~/Desktop/example2/ -name 'abc*.*'
Find files in multiple directories

Searching for Multiple Files and Multiple Patterns

Suppose you want to find multiple files in a directory having .msi and .txt as file types.

Here you need to use both the name and type options on the CLI as:

find -type f \( -name "*.txt" -o -name "*.msi" \)
Search for multiple files and multiple patterns

In a similar approach, you can extend the above command for more files by using extra -o options.

Excluding Certain Files from the Search Results

The find command can also exclude certain types of files from the search result:

find -name '*abc*' -type f \( ! -name '*.msi' \)
Exclude certain files from find search results

Here, the ‘find’ command will look for all the files having ‘abc’ string in their name. However, it will filter out the .msi type of files.

Other Common Examples of the 'find' Command

You have more options that can be used with the ‘find’ command. Let me share a few such examples:

System reporting low disk space? Find bigger files like this:

find -size +2000M

Using the above command, you can find files occupying more than 2000 Megabytes of space.

In case you need to save your findings for later investigation, redirect it to a file:

find -name '*abc*' -type f \( ! -name '*.msi' \) > mysearch.txt
Save the result of the find command

The type option with the find command opens many opportunities.

You can combine it with different file descriptors for different types of files. For example, ‘f’ for a regular file, ‘d’ for a directory, ‘l’ for a symbolic link, etc.

find /var/log -type f -name "*.log" 

Conclusion

In this guide, I explained how to search for files by their names using the find command. You saw multiple ways to narrow down the search path and most importantly, how to incorporate the ‘wildcards’ for pattern searching.

There are many more uses of the find command. Like you can use it to find recently modified files. Here are a few more common examples if you are interested.

15 Super Useful Examples of Find Command in Linux
Learn the super powerful and super useful find command with these practical examples.

You can always search man pages to get extensive insights into the various options with the ‘find’ command.