How can I recursively find all files in the current and subfolders based on wildcard matching?
You can use the find
command in Unix-like systems to recursively find files based on wildcard matching. Here’s how you can do it:
To find files starting with “foo” in the current and subfolders:
find . -name “foo*”
The .
(dot) specifies the starting directory as the current directory.
If you need a case-insensitive search, you can use the -iname
option:
find . -iname “foo*”
This will find files starting with “foo” regardless of case.
You can also find with -exec to perform wildcard matching. This command finds all files (-type f) in the current and subfolders, extracts their basenames (basename {}), and then filters them using grep to match those starting with “foo”.
find . -type f -exec basename {} \; | grep '^foo'
Also, try using ls with recursive globbing (requires Bash 4+). This command enables recursive globbing (shopt -s globstar) and then lists all files and directories matching the pattern “foo*” in the current and subfolders. Note that this method may not handle all edge cases, such as filenames with spaces.
shopt -s globstar ls -d */foo