grep searches text using patterns.
grep [options] "pattern" fileExample:
grep "error" app.logFinds lines containing error.
grep "hello" file.txtSearch for hello.
grep "hello" *.txtSearch all .txt files in the current directory.
grep "hello" file1.txt file2.txtSearch multiple files.
cat file.txt | grep "hello"Search piped input.
grep -i "error" app.logCase-insensitive search. Matches error, Error, ERROR.
grep "Error" app.logCase-sensitive search.
grep -n "error" app.logOutput includes line numbers.
Example output:
42:Database error occurred
grep -r "TODO" .Search all files under the current directory.
grep -R "TODO" .Like -r, but follows symbolic links.
grep -rn "TODO" src/Recursive search with line numbers.
grep -r "main" ~/projectsSearch inside a specific directory tree.
grep -w "cat" file.txtMatches cat, but not category or concatenate.
grep -w "int" *.cFind whole-word int in C files.
grep -x "hello" file.txtMatches only lines that are exactly:
hello
grep -v "debug" app.logShow lines that do not contain debug.
grep -vi "debug" app.logCase-insensitive inverse match.
grep -v "^#" config.iniShow lines that are not comments.
grep -v "^$" file.txtRemove blank lines.
grep -c "error" app.logCount matching lines.
grep -ci "error" app.logCount matching lines, ignoring case.
grep -rc "TODO" src/Count matches per file recursively.
grep -o "error" app.logPrint only the matched part, not the whole line.
grep -o "[0-9]\+" file.txtPrint numbers only.
grep -oE "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}" file.txtExtract email addresses.
grep -l "TODO" *.pyShow filenames that contain a match.
grep -rl "TODO" src/Recursively list files containing TODO.
grep -L "TODO" *.pyShow files that do not contain a match.
grep -h "error" *.logDo not show filenames in output.
Useful when searching multiple files but you only want matching lines.
grep -H "error" app.logAlways show filename, even if searching one file.
grep -q "error" app.logPrint nothing. Useful in scripts.
if grep -q "error" app.log; then
echo "Errors found"
fiBy default, grep uses basic regular expressions.
grep "^ERROR" app.logMatches lines starting with ERROR.
grep "done$" app.logMatches lines ending with done.
grep "^$" file.txtFind blank lines.
grep "^[[:space:]]*$" file.txtFind empty or whitespace-only lines.
grep "h.t" file.txtMatches hat, hot, hit, etc.
grep "[aeiou]" file.txtMatches lines containing a vowel.
grep "[0-9]" file.txtMatches lines containing a digit.
grep "[A-Z]" file.txtMatches lines containing an uppercase letter.
grep "[^0-9]" file.txtMatches lines containing a non-digit.
Use -E for easier regex syntax.
grep -E "cat|dog" file.txtMatches cat or dog.
grep -E "error|failed|fatal" app.logSearch for multiple words.
grep -Ei "error|failed|fatal" app.logSame, case-insensitive.
grep -E "colou?r" file.txtMatches color or colour.
grep -E "[0-9]+" file.txtMatches one or more digits.
grep -E "go+d" file.txtMatches god, good, goood, etc.
grep -E "https?://" file.txtMatches http:// or https://.
grep -e "error" -e "warning" app.logSearch for either error or warning.
grep -E "error|warning" app.logSame using extended regex.
grep -f patterns.txt file.txtRead patterns from a file.
Example patterns.txt:
error
warning
fatal
grep -A 3 "Exception" app.logShow matching line plus 3 lines after.
grep -B 3 "Exception" app.logShow 3 lines before the match.
grep -C 3 "Exception" app.logShow 3 lines before and after.
grep -nC 2 "panic" app.logShow line numbers and 2 lines of context.
grep -r "TODO" --include="*.py" .Search only Python files.
grep -r "TODO" --include="*.go" .Search only Go files.
grep -r "TODO" --include="*.{c,h}" .Search C source and header files.
grep -r "TODO" --exclude="*.min.js" .Exclude minified JavaScript files.
grep -r "TODO" --exclude-dir=".git" .Exclude .git.
grep -r "TODO" --exclude-dir="node_modules" .Exclude node_modules.
grep -r "TODO" --exclude-dir="{.git,node_modules,dist,build}" .Exclude several directories.
Search hidden files
grep -r "token" .May include hidden files depending on shell expansion and directory recursion.
To search hidden files explicitly:
grep -r "token" .[^.]* *Or better:
grep -r "token" . --exclude-dir=".git"Use -F when your pattern contains regex characters and you want them treated literally.
grep -F "user.name" file.txtMatches literal user.name, not user + any char + name.
grep -F "[ERROR]" app.logMatches literal [ERROR].
grep -F "a+b*c" file.txtMatches literal a+b*c.
Available in GNU grep, not always on macOS/BSD.
grep -P "\d+" file.txtMatches digits.
grep -P "\bfoo\b" file.txtMatches whole word foo.
grep -Po "id=\K[0-9]+" file.txtExtracts numbers after id=.
Example input:
user id=123 active
Output:
123
grep -rn "TODO" src/grep -rnE "TODO|FIXME|HACK" src/grep -i "error" app.loggrep -Ei "error|fatal|panic|exception|failed" app.loggrep -nC 3 -i "exception" app.loggrep -v "^#" config.confgrep -Ev "^\s*($|#)" config.confgrep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}" access.loggrep -oE "https?://[^[:space:]]+" file.txtgrep -oE "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}" file.txtgrep -oE "0x[0-9A-Fa-f]+" file.txtgrep -nE "^.{81,}$" file.txtgrep -nE "[[:space:]]+$" file.txtgrep -n $'\t' file.txtgrep -n $'\r' file.txtps aux | grep "nginx"ps aux | grep "[n]ginx"history | grep "docker"ls -la | grep ".txt"netstat -tulpn | grep ":8080"or:
ss -tulpn | grep ":8080"docker ps | grep "postgres"git log --oneline | grep "fix"git diff | grep "^+"find . -name "*.py" -exec grep -n "TODO" {} +Search only Python files.
find . -type f -not -path "./.git/*" -exec grep -n "password" {} +Search files while excluding .git.
find . -type f -name "*.log" -mtime -1 -exec grep -n "error" {} +Search recent log files modified in the last day.
grep exit codes are useful in scripts:
| Exit code | Meaning |
|---|---|
0 |
Match found |
1 |
No match found |
2 |
Error occurred |
Example:
grep -q "success" output.log
if [ $? -eq 0 ]; then
echo "Success found"
else
echo "Success not found"
fiCleaner:
if grep -q "success" output.log; then
echo "Success found"
figrep --color=auto "error" app.logAdd this alias:
alias grep='grep --color=auto'grep "text" binaryfileMay print:
Binary file binaryfile matches
Force text mode:
grep -a "text" binaryfileIgnore binary files:
grep -I "text" *Useful for filenames with spaces.
grep -rlZ "TODO" . | xargs -0 sed -i 's/TODO/DONE/g'Here:
grep -rlZ "TODO" .Outputs matching filenames separated by null bytes.
Use zgrep:
zgrep "error" app.log.gzCase-insensitive:
zgrep -i "error" app.log.gzRecursive compressed search usually requires find:
find . -name "*.gz" -exec zgrep -H "error" {} +| Command | Purpose |
|---|---|
grep |
Standard search |
egrep |
Same as grep -E, mostly obsolete |
fgrep |
Same as grep -F, mostly obsolete |
zgrep |
Search compressed .gz files |
ripgrep / rg |
Faster modern alternative |
ag |
The Silver Searcher, code-search tool |
If you have ripgrep installed:
rg "TODO"It recursively searches by default and respects .gitignore.
Equivalent-ish grep:
grep -rn "TODO" .Search only Go files:
rg -t go "TODO"With grep:
grep -rn --include="*.go" "TODO" .alias grep='grep --color=auto'
alias grepi='grep -i'
alias grepn='grep -n'
alias grepr='grep -rn'
alias grepc='grep -nC 3'| Option | Meaning |
|---|---|
-i |
Ignore case |
-n |
Show line numbers |
-r |
Recursive search |
-R |
Recursive and follow symlinks |
-w |
Match whole words |
-x |
Match whole lines |
-v |
Invert match |
-c |
Count matching lines |
-l |
List files with matches |
-L |
List files without matches |
-o |
Print only matched text |
-h |
Hide filenames |
-H |
Always show filenames |
-q |
Quiet mode |
-E |
Extended regex |
-F |
Fixed string / literal search |
-P |
Perl regex, GNU grep only |
-A N |
Show N lines after |
-B N |
Show N lines before |
-C N |
Show N lines before and after |
--include |
Include matching filenames |
--exclude |
Exclude matching filenames |
--exclude-dir |
Exclude directories |
--color=auto |
Highlight matches |
grep -rn "pattern" .Search recursively with line numbers.
grep -rni "pattern" .Recursive, line numbers, case-insensitive.
grep -rnE "foo|bar|baz" .Search multiple patterns.
grep -rn --include="*.go" "pattern" .Search only Go files.
grep -rn --exclude-dir=".git" --exclude-dir="node_modules" "pattern" .Search while skipping noisy directories.
grep -v "^#" file.conf | grep -v "^$"Show non-comment, non-empty lines.
grep -Ev "^\s*($|#)" file.confSame, cleaner.
grep -oE "[0-9]+" file.txtExtract numbers.
grep -q "pattern" file.txt && echo "found"Check if a file contains a pattern.