How to Search and Find Files Recursively in Linux?

When you search for a file recursively, the search begins in the current directory and then moves on to check all the subdirectories down through the directory tree until there is no sub-directory left.

The “recursive search and find” technique is helpful when you have many files and directories on your system and want to locate a specific file quickly.

This post will list and elaborate on all the methods to search and find files in Linux with the following outline:

  • Using the find Command
  • Using the tree Command

How to Search and Find Files Recursively Using the find Command in Linux?

The ‘find’ command is one of the most straightforward command line tools to look for files recursively. Its basic syntax is explained below:

$ find path -name pattern

Here, the path is the directory where you want to search, whereas the -name specifies the pattern to search for. 

Search for a File Recursively

Below is an example of the find command to search “file1” from the current directory (Home) and its sub-directories.

$ find ~/ -name file1

The above command, when executed, found three instances of “file1” that were being searched using the ‘find’ command.

If you want to do the above in a specified directory, use the ‘cd’ command below to navigate and search the same as above.

$ cd Music

When the ‘cd’ command is executed on the provided folder, you will notice the same directory added next to the hostname, which is “Music,” in our case.

Search and Find Files Recursively Based on Extension

To search and find the files recursively based on their extension, use this format of the ‘find’ command.

$ find ~/ -name “*.txt”

In the output above, the paths and names of the files with the “.txt” extension are printed.

Search and Find Files Recursively Based on Extension and Size

If the files need to be found based on their size, use this format of the ‘find’ command.

$ find ~/ -name "*.txt" -and -size +10k

This will recursively look for files with the .txt extension larger than 10KB and print the names of the files you want to be searched in the current directory. The file size can be specified in Megabytes (M) or Gigabytes (G). Use this command to find only the files above 5MB free of extensions or names.

$ find ~/ -size 5M

The above image shows that all files of the current directory with more than 5MB are printed with their paths.

How to Find and Search Files Using the tree Command in Linux?

Unlike the find command, the ‘tree’ command displays the path, total number of files, and directories where the searched file is in a tree form. It is not pre-installed but can be installed using either of these commands based on your distro.

$ sudo yum install tree             #For CentOS/Fedora
$ sudo sudo apt install tree              #For Debian/Ubuntu-based

Here are examples of using the ‘tree’ command to search and find recursively in Linux by following the below syntax:

$ tree path -P pattern

In the above command, the ‘path’ represents the directory or folder. At the same time, the ‘-P’ specifies the pattern to search for.

Use tree Command to Find a File Based on Extension

Let’s recursively use this command to find the files with the “.txt” extension.

$ tree . -P "*.sh"

Use tree Command to Exclude Specific Pattern

You can use the ‘-I’ option to exclude specific patterns from the search:

$ tree . -P "*.txt" -I "new"

In the above image, the “.” is used to view the content, where the files with the “.txt” extension will be searched in the Music directory.

Use the tree Command to Copy the Output

If you want to copy the output of the tree command (we used the above example) to an output file, use this command.

$ tree . -P “.*txt” -o output.txt

To view the output, use any text editor (we’re using nano editor).

$ nano output.txt

Display Full Path Using the tree Command

The tree command with the flag “-f” displays the full path of each file to be printed, which is used in this format.

$ tree . -P "*.txt" -F

The above image shows the full paths where the searched file is located, making it easier for the user to find it, and more information can be read using this help command for the tree command.

$ tree --help


Finding files from the directories take time unless you know where your file is. If there are many files and directories, then the find and tree commands in Linux allow you to have a recursive search. This post has briefly explained the working/usage of the find and tree commands to search for files recursively in Linux.