Find all files with specific extensions that contains a string
I'd like to know how could I find all files whose extension can be .xml
and .py
that contain the string "Jason" under a path "./" recursively?
Or how could I exclude .po
from the search?
You can do it with grep only (without using find):
grep --include=\*.{xml,py} -Rl ./ -e "Jason"
And to exclude .po:
grep --exclude=*.po --include=\*.{xml,py} -Rl ./ -e "Jason"
Try the following command. It will search only in the .xml
and .py
files for your name:
find . -type f \( -iname \*.xml -o -iname \*.py \) | xargs grep "Jason"
Hope this helps!
This can be done with combining find
and grep
. Here is a small demo - I have test directory with 6 txt and rtf files, two of which contain string "Jason".
CURRENT DIR:[/home/xieerqi/testdir]
$ find . -type f \( -iname "*.txt" -o -iname "*.rtf" \) -exec grep -iR 'jason' {} +
./foo1.txt:Jason
./bar1.txt:Jason
CURRENT DIR:[/home/xieerqi/testdir]
$ ls
bar1.rtf bar1.txt bar2.rtf bar2.txt foo1.rtf foo1.txt foo2.rtf foo2.txt
CURRENT DIR:[/home/xieerqi/testdir]
$ find . -type f \( -iname "*.txt" -o -iname "*.rtf" \) -exec grep -iR 'jason' {} +
./foo1.txt:Jason
./bar1.txt:Jason
We find all the files with txt and rtf extensions here and give them all as parameters to grep. The .
means search in current directory, but you could specify another path and find
will descend into that directory and subdirectories, to search recursively.
Replacing extensions with yours, the final answer is
find . -type f \( -iname "*.xml" -o -iname "*.py" \) -exec grep -iR 'jason' {} +