Wildcard matching is a method, which is used to search for files based on a specific pattern or a set of patterns. When you need to find all files in the current directory and its subfolders based on a wildcard pattern, you can use the ‘find’ command with the ‘-name’ option. The ‘-name’ option allows you to specify the pattern to search for.
In this tutorial, you will learn how to recursively find all files in the current and subfolders based on wildcard matching.
The most common wildcard characters are:
- (asterisk) – matches zero or more characters
- ? (question mark) – matches a single character
- [](square brackets) – matches any character within the specified range or set
Linux Command to Find All Files in Directory and Subdirectory
Using the following methods, you can recursively find all files in the current directory and its subfolders based on wildcard matching in linux:
- Using Wildcards in the “find” Command
- Using Multiple Wildcards in the “find” Command
- Excluding Files and Directories in the “find” Command
Using Wildcards in the “find” Command
The “find” command supports the use of wildcards to match filenames.
For example, if you find all files with the extension “.txt” in the current directory and its subdirectories, you can use the following command:
find . -name "*.txt"
In this command, the “.” specifies the current directory, and the “-name” option specifies that we want to match filenames based on their name. The “*.txt” expression uses the asterisk wildcard to match any string of characters followed by the “.txt” extension.
For another example, to find all the directories and subdirectories, use wildcards at beginning and end of the directory name:
find -name "*tuts*"
Using Multiple Wildcards in the “find” Command
You can use multiple wildcards in the “find” command to match more complex patterns. For example, to find all files with either the extension “.txt” or “.md” in the current directory and its subdirectories, you can use the following command:
find . \( -name "*.txt" -o -name "*.md" \)
In this command, the “-o” option specifies that we want to match files that meet either of the two criteria specified by the expressions in parentheses. The parentheses are used to group the expressions together.
Excluding Files and Directories in the “find” Command
Sometimes, you may want to exclude certain files or directories from the search. For example, you might want to exclude hidden files or directories from the search results. To do this, you can use the “-prune” option in combination with the “-o” option. Here’s an example:
find . \( -name "*.txt" -o -name "*.md" \) -not \( -path "./.*" -prune \)
In this command, the “-path” option specifies the pattern to match against the path of the file or directory, and the “-prune” option tells “find” to exclude any directories that match the pattern. The “-not” option is used to negate the expression so that “find” will include all files and directories that do not match the pattern.
Conclusion
Using the “find” command in Linux is a powerful way to search for files and directories based on wildcard matching. By using wildcards and other options, you can narrow down your search to find exactly what you’re looking for. With a little practice, you’ll be able to use the “find” command to search for files in a wide range of situations.