Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you grep through code that lives in many different directories?

Tags:

I'm working on a Python program that makes heavy use of eggs (Plone). That means there are 198 directories full of Python code I might want to search through while debugging. Is there a good way to search only the .py files in only those directories, avoiding unrelated code and large binary files?

like image 403
joeforker Avatar asked May 14 '09 19:05

joeforker


People also ask

How do I grep in multiple directories?

To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename. In the example below, we also added the -w operator to show whole words, but the output form is the same.

Can grep be instructed to look at every file in a directory and its subdirectories?

You can make grep search in all the files and all the subdirectories of the current directory using the -r recursive search option: grep -r search_term .

How do I grep all files in a directory recursively?

Recursive Search To recursively search for a pattern, invoke grep with the -r option (or --recursive ). When this option is used grep will search through all files in the specified directory, skipping the symlinks that are encountered recursively.

How do I search for a directory in grep Linux?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.


2 Answers

find DIRECTORY -name "*.py" | xargs grep PATTERN 

By the way, since writing this, I have discovered ack, which is a much better solution.

(And since that edit, I have discovered ag).

like image 101
Steve B. Avatar answered Nov 08 '22 22:11

Steve B.


grep -r -n "PATTERN" --include="*.py" DIRECTORY 
like image 31
monowerker Avatar answered Nov 08 '22 22:11

monowerker