To recursively find all files in the current directory and subfolders based on wildcard matching in Linux, use the find
command with the -name
or -iname
(case-insensitive) flag. Here’s how:
Basic Syntax
find [starting-directory] -type f -name "wildcard-pattern"
-type f
: Restrict results to files (excludes directories).-name "pattern"
: Case-sensitive wildcard matching.-iname "pattern"
: Case-insensitive wildcard matching.
Examples
1. Find all .txt
files recursively:
find . -type f -name "*.txt"
2. Find .jpg
files (case-insensitive):
find . -type f -iname "*.jpg" # Matches .JPG, .jPg, etc.
3. Find files named app.log
in /var/log
:
find /var/log -type f -name "app.log"
4. Find files matching a complex pattern (e.g., data_2023*.csv
):
find . -type f -name "data_2023*.csv"
Key Notes
- Wildcards:
*
: Matches any number of characters (e.g.,*.log
).?
: Matches a single character (e.g.,file_202?.txt
).- Quotes: Always wrap the pattern in quotes (
"*.txt"
) to prevent shell expansion. - Hidden Files: Include
.
in the pattern to find hidden files (e.g.,-name ".*"
).
Advanced Use Cases
Find and list files with ls
-like formatting:
find . -type f -name "*.py" -ls
Find and delete matching files (use with caution!):
find . -type f -name "*.tmp" -delete
Find files modified in the last 7 days:
find . -type f -name "*.log" -mtime -7
Alternative: grep
with ls
(Not Recommended)
While you can use ls -R
to list files recursively, it’s less flexible and error-prone for wildcards:
ls -R | grep "\.txt$" # Lists all .txt files (may break with spaces)
Summary
Use find
for reliable, recursive wildcard searches:
find . -type f -name "your-pattern"