howtouselinux

6 Examples to Find Files By Name in Linux

Table of Contents

The best way to find files by name in Linux is using the find command with the “-name” option.  This command will search through the directories for files that have the specific word in their name. This can be very useful when you need to find a specific file and don’t know where it is located.

In this blog post, we will discuss 6 examples of how to use the find command to search for files by name. We will also show you how to pipe the output of the find command so that you can easily find the information that you need. Let’s get started!

The following Linux commands can be used to search files by name.

  • find /path -name *.txt
  • find /path -type f -name test.txt
  • find /path -name failed*.* -type f
  • find /path -type f -not -name “*.html”
  • find / -name “file.txt” -size +4M
  • find /dev/ -type b -name “sda*”

 

Find command in Linux

Linux find command is a powerful tool that can be used to locate and manage files and directories based on a wide range of search criteria. This post will cover how to find file by name in Linux.

When using find, we would follow the syntax below.

find [options] [path] [expression]

  • options: This is optional. We can leave this out most of the time. We can read the manual page for all the options the find command has.
  • path: This is the directory we want to search.
  • expression: This is where we place our search criteria for what we want to find whether by name, or file size etc.

 

You can also pipe the output of the find command to another program, such as grep, in order to filter the results. For example, if you only want to see files that have a certain extension, you can use the following command:

find path -name “filename” | grep “extension”

In this case, the grep program will only print out lines that contain the word “extension”.

How to Use the find Command

To search for files based on a specific filename, you can use the “find” command with the “-name” option. For example, if you want to find all of the files that have the word “file” in their name, you can run the following command:

“find . -name ‘*file*'”

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name.

If you want to find all of the files that have the word “file” at the beginning of their name, you can use the following command:

“find . -name ‘file*'”

This command will search through the current directory and all of its subdirectories for files that have the word “file” at the beginning of their name.

Advanced options in Find command

The “find” command also allows you to use advanced search options to filter results. You can use the “find” command with the “-type” option to search for files of a specific type. You can use the “find” command with the “-mtime” option to search for files that have been modified in a certain amount of time. You can use the “find” command with the “-maxdepth” option to specify how deep you want to search into directories.

For example, if you want to find all of the PDF files that are older than one week, you can run the following command: “find . -type f -mtime +7”

This command will search through the current directory and all of its subdirectories for PDF files that have been modified more than seven days ago.

Find All Files With A Certain Name in Linux

In this example, we will use the find command to search for all files with a certain name. For this example, we will search for all files with the name “test.txt”.

To do this, we will use the following command:

find / -name “test.txt”

This command will search through all of the directories on your system for a file named “test.txt“. The output of this command will look something like this:

/home/user/test.txt
/tmp/test.txt

As you can see, this command was able to find two files with the name “test.txt”. One file was in the user’s home directory and the other file was in the /tmp directory.

To list all files in the current directory, we can use this command. ./ means current directory here. find ./

If we need to list the file which name is ‘test’, we can use this command. find ./ -name ‘test’

Absolute path is a complete path from the start of the actual filesystem from / directory.To list all files in the file system with a specified base file name, file type:

find /opt -name .profile -print

This command searches the /opt directory and prints the complete path names of all files named .profile.

The /opt (slash) instructs the find command to search the /opt directory and all of its subdirectories. In order not to waste time, it is best to limit the search by specifying the directories where we think the files might be.

Check this post to learn more about file path.

Find Files with a name pattern in Linux

We can use basic shell wildcard characters to broaden our search. For instance, the asterisk (*) represents any number of characters:

$ find ~ -iname “foo*”
/home/tux/Documents/examples/foo
/home/tux/Documents/examples/Foo
/home/tux/Documents/examples/foo.xml
/home/tux/Documents/examples/foobar.txt

A question mark (?) represents a single character:

$ find ~ -iname “foo*.???”
/home/tux/Documents/examples/foo.xml
/home/tux/Documents/examples/foobar.txt

This isn’t regular expression syntax, so the dot (.) represents a literal dot in this example.

If there is more than one file which name has ‘test’ in it like test1, test2, we can use this command. find ./ -name ‘*test*’

If we want to search and list all files with a given name in multiple directories, we can either start the search at root directory, or if we know the directories, we can specify them.

Example: find ./test ./logs -name failed*.* -type f

Sample output:
./test/failed_tests.txt
./logs/failed_tests.log

*
An asterisk is replaced by any number of characters in a filename. For example, ae* would match aegis, aerie, aeon, etc. if those files were in the same directory. You can use this to save typing for a single filename (for example, al* for alphabet.txt) or to name many files at once (as in ae*).

?
A question mark is replaced by any single character (so h?p matches hop and hip, but not help).

 

Find Multiple Files by name in Linux

Here is a little complex example. This command will remove all files named a.out or *.o that are not accessed for a week and that are not mounted by using nfs.

find / \( -name a.out -o -name ‘*.o’ \) -atime +7 ! -fstype nfs -exec rm {} \;

Note: The number that is used within the -atime expression is +7. It is the correct entry if we want the command to act on files that are not accessed for more than a week (seven 24-hour periods).

Find Files not matching a name pattern in Linux

This Linux find command using the “not” operator creates a list of all files not ending with the .html file extension (filename pattern).

find . -type f -not -name “*.html”

We can also use the following command to get this. find . -type f ! -name “*.html”

Excluding Files and Directories in Find command

You can use the “find” command with the “-exclude” option to exclude certain files and directories from your search.

For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the PDF files, you can run the following command:

find . -name ‘file*’ -exclude *.pdf

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the PDF files.

You can also use the “-exclude-dir” option to exclude certain directories from your search. For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the files in the “tmp” directory, you can run the following command:

“find . -name ‘file*’ -exclude-dir tmp

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the files in the “tmp” directory.

Find command and other options in Linux

  • find / -name “file.txt” -size +4M
  • find /dev/ -type b -name “sda*”
  • find / -type d -name “a.txt”
  • find /opt -type f -name ‘howtouselinux’ -mtime +1

Related:

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.