The other day, i needed to look for a string recursively in my directory structure – but only in in files with .py extension. (because there were other subfolders containing images etc, which i wanted to ignore).
- It seems grep has an option for that : –include
Options I find useful:
 -r : Read all files under each directory, recursively, following symbolic links only if they are on the command line
 -i : Ignore case distinctions in both the PATTERN and the input files
 -n : Prefix each line of output with the 1-based line number within its input file
 -a : Process a binary file as if it were text
 -l : Suppress normal output; instead print the name of each input file from which output would normally have been printed.
 -h : Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search
Example using a zip directory:
$ unzip -l _RawData.zip
$ unzip _RawData.zip -d _RawData
$ grep -rinal India _RawData
$grep India _RawData/900.csv _RawData/924.csv Note: am searching for word containing substring India in 2 files
 For BSD or GNU grep you can use
-B num to set how many lines before the match and
-A num for the number of lines after the match.
grep -B 3 -A 2 foo README.txt
If you want the same number of lines before and after you can use
grep -C 3 foo README.txt
This will show 3 lines before and 3 lines after.
egrepis 100% equivalent to
- Another possibility when you’re just looking for any of several patterns (as opposed to building a complex pattern using disjunction) is to pass multiple patterns to
grep. You can do this by preceding each pattern with the
grep -e foo -e bar *.txt