Open In App

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

Improve
Improve
Like Article
Like
Save
Share
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:

 

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"

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. 


Last Updated : 02 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads