Updated: 23 February 2025
Find all occurrences of foo in directories and files recursively, from the current directory
# -i: case insensitive
# -r: recursive but don't follow symlinks
# -n: show line number
# --text: treat binary files as text
grep -irn --exclude-dir=dir --text foo *Interpret PATTERNS as --fixed-strings, not regular expressions
grep -F './dir/dir2' file.txtRead-in a long regex, from a file
cat grep-me.log | grep --file=long-regex.txtWord boundary
Search for the whole word 96730
grep "\<96730\>" app.logMultiple patterns
Search for several patterns at once
grep 'pattern1\|pattern2\|pattern3' log.txtSurrounding context
Find tell in trans.sql and show 3 lines of context surrounding the result
grep -C 3 tell trans.sqlShow 3 context lines before and 2 lines after matches
grep -B 3 -A 2 bar index.phpShow --only-matching parts of the matching line
grep -o foo index.php