Open In App

How to Recursively Find all Files in Current and Subfolders Based on Wildcard Matching in Linux

Last Updated : 19 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Traversing through a directory tree to locate files is a common operation performed by most filesystem management software. This utility is in the form of command-line commands in most Operating Systems. In this article, you will learn how to find files using Wildcard Matching in Linux Operating System.

Recursively finding files in a directory tree

The use of the find command would be made for performing the task. The command 's' is used to search for files in a directory hierarchy. The help page of the find command is as follows:

 

Syntax

The find command and the '-name' switch will look for the desired file in the directory. The switch provides a name (or pattern) for the find command to look out for. Hence, the final command would be

find -name "Re_Pattern"

Here:

  • /path/to/directory: The directory where you want to search.
  • -name: The option that specifies the pattern to match.
  • "pattern": The file name or pattern you're searching for. You can use wildcards such as * and ? to define flexible search criteria.

Wildcards in Linux

Any file with the "Re_Pattern" in its filename would be displayed as a result. The pattern could either contain literal characters or a sequence of Wildcard expressions. The two Wildcards that are used in such patterns are:

*   => Any character any number of times (may not even be present)
?   => One character should be present

The directory in which the operations would be performed is :

 A directory containing some files and sub-directories

For simplicity, the subdirectories are empty. Hence, any effect produced would be visible only on the file in this directory. The file that would be searched for within the directory would be "apple" the command for which would be:

find -name "apple"
 

Since only one file in the directory had the name apple, only 1 name was shown in the output. But if you include a wildcard along it, such as * (Asterisk):

find -name "*apple*"
 

we ended up with the result of all files containing apple in its name. Similarly, a combination of such wildcards could be used. Ex. If we want to obtain all the files which have 4 characters in their name, the command would be:

find -name "????"

Hence, using these two wildcards, an array of patterns could be made for recognizing several filenames. The find command contains other switches which allow for more output streamlining. 

Conclusion

The find command in Linux, combined with wildcard matching and advanced options, offers powerful functionality for locating and managing files. Whether you're searching based on file names, extensions, sizes, or timestamps, the command provides flexibility and control over your file system.


Next Article

Similar Reads