Linux Commands

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

Finding a specific file through a terminal in Linux is one of the common operations. Most of the files management systems use it. This tutorial covers finding single file, multiple files, folder, and subfolder recursively in Linux based on Wildcard.

What are Wild Cards

Wildcards are commonly used in computer programming and in various computer applications, such as text editors, search engines, and command-line interfaces.

They can be used to match or search for specific patterns or characters in a file or a string. Wildcards can also be used to create more flexible and powerful search queries, making it easier to find and manipulate the desired data.

Types of Wild Cards

Examples of common wildcard characters include the asterisk (*) and the question mark (?).

  • Asterisk (*): The asterisk can be used to check for the files that match character sequences.
  • Question mark (?): The question mark can be used to match any single character.

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

Finding all files in a directory and its subdirectories that match a specific pattern can be done using the “find” command in Linux. The “find” command can search files based on various criteria, such as name, permissions, type, and number of characters in file name.

To learn more about find command run the given command:

$ man find

1: Finding Files Using Asterisk (*) Wildcard

To recursively find all files in the current directory and its subdirectories that match a wildcard pattern, you can use the following find command and below is the syntax for it:

$ find -name “file-name”

The “” is a wildcard that matches any characters, so this command will find all files with names that end in “file-name”.

To find a specific directory:

$ find -name “linuxhint1”

To find all the directories with same name we will use a wildcard at the end of directory name:

$ find -name “linuxhint*

To find all the directories and subdirectories, use wildcards at beginning and end of the directory name:

$ find -name*linuxhint*

2: Finding Files Using Question mark (?) Wildcard

To find files with a specific number of characters then “?” wild card can be used, for example, if we want to find directories that contains 4 unknown characters then use:

$ find -name “????”

Another way of finding recursively all files in current and subfolders based on wildcard matching in Linux pipe grep with find command:

$ find -print | grep -i linuxhint

Finding Files Using tree Command

The tree command is another useful command to find files and directories. To install tree utility use:

$ sudo apt install tree

After installing use:

$ tree -P linuxhint

For more help about tree command, run:

$ man tree

Conclusion

This article covers a few examples of using the “find” command with Wildcards Question mark (?) and Asterisk (*). Using these two Wildcards we can create complex search patterns that can help you quickly locate specific files on your system. Lastly, we also cover the tree command that can find the files, folders and subfolders recursively in Linux. Using tree command a tree can be drawn for better illustration of all files inside a single directory.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.