grep
grep for a string, include lines before and after
cat FILE | grep 'FINDTHIS' -B NUM-LINES-BEFORE -A NUM-LINES-AFTERRead lines from file A as patterns to match in file B
grep -F -f a.txt b.txtor patterns that don’t match
grep -Fv -f a.txt b.txt-F treats the strings as “fixed” with literal interpretations of characters like .
Example
└─$ cat alpha.txt
a
b
c
d
e
f
g
h
i
└─$ cat vowel.txt
a
e
i
└─$ grep -F -f vowel.txt alpha.txt
a
e
i
└─$ grep -Fv -f vowel.txt alpha.txt
b
c
d
f
g
hSearch recursively through a folder
grep -rniI --exclude-dir=.git 'pattern' /path/to/search-r recurse, -n line numbers, -i ignore case, -I skip binary files.
Limit to certain files, or just list which files match:
grep -rn --include='*.md' 'pattern' .
grep -rl 'pattern' .See also
- ripgrep — faster, recursive by default.
- Search Files and Filenames — finding files by name as well as by content.
- Search Inside Archives