10

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?

6 Answers 6

12

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"
1
  • Simple, to the point. Works well. Might I suggest error output suppression for files it can't read through? +1
    – Terrance
    Sep 28, 2015 at 18:38
4

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!

2
  • Beaten me by a second :) I'm using -exec though Sep 28, 2015 at 18:33
  • @Serg LOL. I do like your answer by the way! +1
    – Terrance
    Sep 28, 2015 at 18:33
2

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' {} + 
1
  • I like it Serg. =) It runs quickly on my system as well. Results were returned in a few seconds.
    – Terrance
    Sep 28, 2015 at 18:49
0

You could do this:

grep -r "Jason" ./ | grep .xml; grep -r "Jason" ./ | grep .py

That will display the output of any files containing Jason and limit it to filenames containing .xml or .py.

If you want to just exclude .po, then try this:

grep -r "Jason" | grep -v .po
2
  • Hmm, i run the first line and it throws everything that contains py (all filers called py) and also didn't exclude .po Sep 28, 2015 at 18:13
  • 1
    I would not do this because it searches in all files and that can take a long time. Why looking in files you know you do not want. Grep should be limited to search only in .xml and .py files.
    – nobody
    Sep 28, 2015 at 18:41
0

How I understand from your question above, u need to show all files with "Jason" string into. So, may be this can help you:

find . -type f \( -name "*.xml" -o -name "*.py" \) | grep -r "Jason" | cut -d':' -f1 | uniq
0

The best solution probably has been given already by nobody, however when the tool doesn't support searching a directory recursively for filenames one can enable the globstar option of bash and perform a recursive globbing:

shopt -s globstar; grep -F -l Jason **/*.{xml,py}
user@user-X550CL:~/tmp$ tree -a
.
└── dir1
    └── dir
        ├── containing_Jason.py
        ├── containing_Jason.txt
        ├── containing_Jason.xml
        ├── not_containing_Jason.py
        ├── not_containing_Jason.txt
        └── not_containing_Jason.xml

2 directories, 6 files
user@user-X550CL:~/tmp$ shopt -s globstar
user@user-X550CL:~/tmp$ grep -F -l Jason **/*.{xml,py}
dir1/dir/containing_Jason.xml
dir1/dir/containing_Jason.py

The equivalent feature in zsh is enabled by default:

grep -F -l Jason **/*.{xml,py}
user@user-X550CL ~/tmp % tree -a                       
.
└── dir1
    └── dir
        ├── containing_Jason.py
        ├── containing_Jason.txt
        ├── containing_Jason.xml
        ├── not_containing_Jason.py
        ├── not_containing_Jason.txt
        └── not_containing_Jason.xml

2 directories, 6 files
user@user-X550CL ~/tmp % grep -F -l Jason **/*.{xml,py}
dir1/dir/containing_Jason.xml
dir1/dir/containing_Jason.py

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .