find command in Linux with examples

If you need to search for one or more particular files or directories, the find command in Linux is the perfect tool for the job. The find command can search for a file with a specific name, but you can also search for files that follow certain naming patterns. This can be broadened all the way to finding files based on file size, file extension, or a lot of other options.

The find command is very versatile and comes packed with tons of options. It may take a little time to learn how to use it, but in the end, it’s much more useful then searching for files manually or via GUI. You can also instruct the find command to execute another command on any files that match a certain search criteria. Whether you are searching for a file in your current directory, one of its subdirectories, or just anywhere on your entire system, the find command command is up to the task.

In this tutorial, you’ll learn how to use the find command in Linux through examples. Follow along below to see learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the find command on Linux
find command in Linux with examples
find command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software find
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

It may seem like the find command in Linux has a confusing syntax when you glance at a sample command for the first time. Indeed, it does have a lot of options and some of these can get pretty complex. But don’t worry, we’re here to break down all the tricky parts of the find command. Follow along to learn how the command works and you will be an expert at it in no time.

find command in Linux Basic Examples

  1. Let’s start with the simplest syntax possible, which would be searching for a file by its name in our present working directory. To do so, we will instruct find to search the . directory and specify a file name with the -name option.
    $ find . -name "example.txt"
    
    The find command indicates that it was able to locate example.txt in our current directory
    The find command indicates that it was able to locate example.txt in our current directory

    A couple of things to note here: this command will not only search our present working directory, but also all of its subdirectories (even if the structure is very deep). Furthermore, it will match any files OR directories that have the name we specified.

  2. Next, let’s try searching a different directory for a particular type of file. In this example, we will search the /home directory (and its subdirectories) for any file names ending with .png.
    $ find /home -name "*.png"
    

    Our find command has returned several png files
    Our find command has returned several png files
  3. You can use the -type option to instruct find to only search for files or directories. If you don’t specify this option, the command will return results for both.Search for files only:
    $ find /home -type f -name "example"
    

    Search for directories only:

    $ find /home -type d -name "example"
    

    Separate results appear when we search only for files and then only for directories
    Separate results appear when we search only for files and then only for directories
  4. Use the -iname option in place of -name to make your search case insensitive.
    $ find /home -type f -iname "example.txt"
    
  5. If you don’t want the find command to traverse too deeply into subdirectories, you can specify a limit with the -maxdepth option. For example, this command will limit find to a depth of two subdirectories:
    $ find . -type f -maxdepth 2 -name "example.txt"
    
  6. Now that you are starting to understand how the find command and its options work, let’s combine a couple of new ones in this next command. The example below will find all .conf files that have been modified in the last seven days, are owned by user linuxconfig, and exist in that user’s home directory.
    $ find /home/linuxconfig -type f -user linuxconfig -mtime -7 -name "*.conf"
    
  7. The find command can automatically delete files it finds if you specify the -delete option. Be very careful with this option, and be sure to first run the find command without it so you know exactly what it plans to delete.
    $ find . -type f -name "*.tmp" -delete
    
NOTE
You can always use the man command to read more about the find command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

The most advanced usage of the find command involves using it to execute other commands on files that match a certain search criteria. Some of the syntax is extremely confusing to newcomers. Even seasoned Linux users will sometimes need to refer back to these examples because it’s hard to remember exactly where a semicolon or other uncommon character is supposed to go inside the command. Don’t worry, we will explain it in the following examples and it will start making sense.

find command in Linux Advanced Examples

  1. The find command’s functionality can be further extended with the -exec option. Using this option allows you to execute a command on every file that find finds. For example, let’s change the file permissions to 750 for every file found:
    $ find . -type f -name "*.conf" -exec chmod 750 '{}' \; -print
    

    In the above command, '{}' is a placeholder for the files that are found with find. The -exec option is terminated with a semicolon, which must be escaped, hence the \;. The -print option will output all the file names and paths to your terminal.

  2. Depending on the command that you are executing inside of the -exec parameters, it is sometimes more efficient to use a plus sign in place of the semicolon. Doing so will allow find to execute the command on multiple files simultaneously. This is opposed to the previous example, in which only one file had its permissions changed at a time.
    $ find . -type f -name "*.conf" -exec chmod 750 '{}' + -print
    

    In this particular example, you will indeed see a huge speed increase. But depending on the command you are executing, your results could vary. It’s best to test out both methods.

Closing Thoughts

In this tutorial, we learned all about the find command on Linux. The find command is extremely powerful at searching for files and directories because of the sheer number of options that are baked into the command. We are able to tell the command to search based on very specific criteria, such as the owner of a file and when it was last modified, allowing us to really fine tune our search results.

As if this wasn’t already enough, the -exec option further extends the command’s usefulness. And, believe it or not, we are still only scratching the surface. The find command can also be used in conjunction with xargs to do even more things. It’s also well worth your time to check the manual page for find because there are many more niche options that we have not covered here.