Get list of all files by mask in terminal
Solution 1
You can use -o
for "or":
find . -path '*/trunk/src/*.h' -o -path '*/trunk/src/*.cpp'
which is the same as
find . -path '*/trunk/src/*' \( -name '*.h' -o -name '*.cpp' \)
If you want to run grep on these files:
find . \( -path '*/trunk/src/*.h' -o -path '*/trunk/src/*.cpp' \) -exec grep PATTERN {} +
or
find . -path '*/trunk/src/*' \( -name '*.h' -o -name '*.cpp' \) -exec grep PATTERN {} +
Solution 2
In bash, turn on the globstar option so that **
matches any level of subdirectories. You can do this from your ~/.bashrc
. Also turn on the extglob
options to activate ksh extended patterns.
shopt -s globstar extglob
Then:
grep PATTERN **/trunk/src/**/*.@(h|cpp)
Beware that bash versions up to 4.2 follow symlinks to directories when you use **
.
Zsh makes this easier, you don't need to set any options and can just type
grep PATTERN **/trunk/src/**/*.(h|cpp)
If the command line is too long, and you're on Linux or other platform with GNU grep, you can make grep
recurse instead of the shell to save on the command line length.
grep -R --include='*.cpp' --include='*.h' PATTERN **/trunk/src
Related videos on Youtube
Loom
Updated on September 18, 2022Comments
-
Loom over 1 year
I want to find all
*.h
,*.cpp
files in folders with defined mask, like*/trunk/src*
. So, I can find separately*.h
and*.cpp
files:find . -path "*/trunk/src/*.h" find . -path "*/trunk/src/*.cpp"
What is the best way to get the file-list both of types (
*.h
and*.cpp
)?PS I'd like to pipe the list to
grep
.