On this page 16 sections
- What grep does
- Ignore case and show line numbers
- Recursive searches
- Exact strings
- Match whole words or complete lines
- Invert matches
- Extended regular expressions
- Print only the matching part
- Context around matches
- Count results
- List only matching filenames
- Include and exclude files
- Search several patterns
- Read patterns from a file
- Useful pipelines
- Diagnose a checkout error from an application log
What grep does
grep searches input for lines matching a pattern. It can read files, standard input or entire directory trees and supports both simple strings and regular expressions.
grep "error" application.logIgnore case and show line numbers
-i makes matching case-insensitive, while -n prints the line number of every match.
grep -i "error" application.loggrep -n "Failed password" /var/log/auth.loggrep -in "warning" application.logRecursive searches
-r searches recursively through directories. -R behaves similarly but follows symbolic links.
grep -r "password" /etc 2>/dev/nullgrep -rin "api_key" /var/www 2>/dev/nullExact strings
-F treats the search pattern as a fixed string instead of a regular expression. This is safer and faster when searching for text containing regex characters.
grep -F "10.20.20.10" access.logMatch whole words or complete lines
-w restricts matches to whole words while -x requires the complete line to match.
grep -w "root" /etc/passwdgrep -x "PermitRootLogin no" /etc/ssh/sshd_configInvert matches
-v returns lines that do not match the pattern. This is useful for removing noise from command output.
grep -v "^#" /etc/ssh/sshd_configgrep -Ev "^\s*(#|$)" /etc/ssh/sshd_configExtended regular expressions
-E enables extended regex syntax, including alternatives with | and grouping with parentheses.
grep -Ei "error|warning|critical" application.loggrep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' access.logPrint only the matching part
-o outputs only the text that matched rather than the entire line. This is extremely useful when extracting IPs, domains, hashes or identifiers.
grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' access.loggrep -Eo '([A-Za-z0-9_-]+\.)*sam0x\.me' urls.txtContext around matches
-A prints lines after a match, -B prints lines before and -C prints both directions.
grep -A 3 "ERROR" application.loggrep -B 2 "connection refused" application.loggrep -C 3 "panic" application.logCount results
-c returns the number of matching lines. When combined with multiple files it prints a count per file.
grep -c "Failed password" /var/log/auth.logList only matching filenames
-l prints the files containing at least one match. -L does the opposite and prints files without matches.
grep -ril "password" /var/www 2>/dev/nullgrep -L "ServerName" /etc/apache2/sites-enabled/*Include and exclude files
Recursive searches can be restricted by filename using --include or --exclude. Entire directory trees can be removed with --exclude-dir.
grep -rin --include="*.php" "password" /var/wwwgrep -rin --exclude="*.log" "sam0x" /optgrep -rin --exclude-dir=node_modules "API_KEY" .Search several patterns
-e can be repeated to specify several independent search patterns.
grep -e "ERROR" -e "CRITICAL" -e "FATAL" application.logRead patterns from a file
-f loads one search pattern per line from another file. This is useful when maintaining larger lists of indicators or keywords.
grep -f indicators.txt access.logUseful pipelines
grep becomes especially useful when filtering the output of other commands. It is commonly combined with ps, ss, journalctl, find and command histories.
ps aux | grep "[s]sh"ss -lntp | grep ":443"journalctl -k | grep -i "error"Diagnose a checkout error from an application log
A checkout request returned HTTP 503 after a deployment. The log also contains an unrelated database warning from another request. Save this synthetic fixture as checkout.log to work through the diagnosis.
The result is line 4, which identifies the failed request as cart-42. Use that ID to reconstruct the request independently of other activity in the same file.
The expected matches are lines 1, 3 and 4. The database warning on line 2 belongs to cart-63. The first follow-up for this sample is to check inventory service latency and health around 12:01:06. The log shows which dependency timed out, while the dependency's own logs and metrics can help identify the cause.
12:01:04 INFO request_id=cart-42 POST /checkout
12:01:04 WARN request_id=cart-63 database retry
12:01:06 WARN request_id=cart-42 inventory timeout after 2000ms
12:01:06 ERROR request_id=cart-42 checkout returned 503grep -nF 'returned 503' checkout.loggrep -nFw 'request_id=cart-42' checkout.log