How to Search Files Recursively into Subdirectories?

In Linux, the word recursive is used to consider the directories, their content, and all the subdirectories alongside their content. Recursive searching has significance in tracking the file based on name, format, and size in the subdirectory of the current system.

This guide will explain various methods to search files recursively into subdirectories. The content of this article is mentioned-below:

Method 1: Using the “find” Command

The “find” command searches these files and displays the hierarchy path. The general syntax of the “find” command is given below:

Syntax

$ find [path] [options] [expression]

The description of the above syntax is explained in the enlisted format:

  • path: It specifies the directory where searching starts.
  • option: It represents the filtering criteria such as permission, date, and time.
  • expression: It identifies the action which performs with the file.

Let’s practice the “find” command.

Example 1: Search a Particular File Recursively

The “find” command is utilized to search a particular file recursively through the “name” utility. For instance, the below script will search out the “office.txt” file:

$ find ~/ -name office.txt

The output confirms that the “office.txt” file has been recursively searched from the current directory to the

Example 2: Search Multiple Files Recursively

The “find” command assists Linux users in searching multiple files based on their names. This command requires the “name” utility that matches the file name in the directory and fetches out the complete path. For instance, the below script will explore two files, “empfile.txt” and “office.txt”, recursively:

$ find . \( -name empfile.txt -o -name "office.txt" \)

After executing the above script, you can display the path directories of the different files.

Example 3: Search Specific File Format

To search out the specific file format, the extension of a particular file is required to display the path directory in the operating system. For instance, the script below will search all files with the specific file format:

$ find ~/ -type f -name "*.sh"

The output confirms that only one file of the “.sh” file format is present in the current system directory.

Example 4: Search Specific File Size

To search out the specific file size, you can follow the “find” command with the “-size” utility to display all files that meet the requirement. In our case, the “var” directory is specified to display all files whose size is greater than 10 MB:

$ sudo find /var -size 10M

The output verifies that there are three Debian package files in the “var” directory having a size greater than 10 MB.

Method 2: Using the “tree” Command

The tree command lists all directories, sub-directories, and their files. In our case, specify the particular file name for displaying in the terminal window with tree-pattern:

$ tree -P office.txt

You can verify that the “office.txt” file has been searched out recursively in sub-directories.

Method 3: Using the “du” Command

The “du” command specifies the disk usage of all files, directories, and sub-directories. For instance, it can also be utilized to search the files recursively with their sizes as below:

$ du -hs ~/*

You can visualize all files, directories, and sub-directories with their sizes in the above figure.

This is all about recursively searching files in Linux.

Conclusion

In Linux, the “find”, “tree”, and “du” commands can be used to search files recursively into subdirectories. Using the “find” command, you can search single and multiple files with specific file formats, sizes, and many more. Additionally, the “du” command can also be used to display the file name in different subdirectories having different disk usage. This guide has explained all possible methods to search files recursively into subdirectories.