grep "<pattern>" <file>Prints every line containing the pattern.
Example
grep "ERROR" app.logSearches text for lines matching a pattern, with regular expressions, recursion and context.
sudo apt install grepgrep "<pattern>" <file>Prints every line containing the pattern.
Example
grep "ERROR" app.loggrep -i "<pattern>" <file>Case-insensitive match.
Example
grep -i "error" app.loggrep -v "<pattern>" <file>Inverts the match: prints lines that do not contain the pattern.
Example
grep -v "DEBUG" app.loggrep -n "<pattern>" <file>Prefixes each match with its line number.
Example
grep -n "TODO" main.cgrep -c "<pattern>" <file>Counts matching lines instead of printing them.
Example
grep -c "Failed password" /var/log/auth.loggrep -w "<word>" <file>Matches whole words only, so “root” does not match “chroot”.
Example
grep -w "root" /etc/passwdgrep -x "<line>" <file>Matches only if the entire line is the pattern.
Example
grep -x "PermitRootLogin yes" /etc/ssh/sshd_configgrep -r "<pattern>" <dir>Searches every file under a directory recursively.
Example
grep -r "password" /etcgrep -rl "<pattern>" <dir>Lists only the names of files that match.
Example
grep -rl "api_key" ~/projectsgrep -rn --include="<glob>" "<pattern>" <dir>Restricts the search to certain file types.
Example
grep -rn --include="*.php" "eval(" /var/wwwgrep -r --exclude-dir=<dir> "<pattern>" .Skips directories you do not care about.
Example
grep -r --exclude-dir=node_modules "TODO" .grep -rIl "<pattern>" <dir>Skips binary files (-I) — here hunting for private keys left on disk.
Example
grep -rIl "BEGIN RSA PRIVATE KEY" / 2>/dev/nullgrep -E "<a>|<b>" <file>Extended regular expressions: alternation, +, ? and {n} without backslashes.
Example
grep -E "error|fail|denied" sysloggrep -o "<regex>" <file>Prints only the matched part of each line — here, every IPv4 address.
Example
grep -oE "[0-9]{1,3}(\.[0-9]{1,3}){3}" access.loggrep -F "<string>" <file>Treats the pattern as a fixed string, not a regex — faster and no escaping.
Example
grep -F "a.b*c" notes.txtgrep -P "<regex>" <file>Perl-compatible regex with \d, lookarounds and lazy quantifiers (GNU grep).
Example
grep -P "\d{4}-\d{2}-\d{2}" app.loggrep -f <patterns> <file>Reads many patterns from a file, one per line — handy for IOC matching.
Example
grep -f iocs.txt proxy.loggrep -A <n> "<pattern>" <file>Shows n lines after each match.
Example
grep -A 3 "Exception" app.loggrep -B <n> "<pattern>" <file>Shows n lines before each match.
Example
grep -B 2 "segfault" kern.loggrep -C <n> "<pattern>" <file>Shows n lines either side.
Example
grep -C 5 "Accepted password" auth.log<command> | grep "<pattern>"Filters the output of another command.
Example
ps aux | grep sshdA stream editor for filtering and transforming text: substitutions, deletions and in-place file edits.
A pattern-scanning language for column-based text: extract fields, compute totals and reformat output.
Searches the directory tree by name, type, size, time, owner or permissions — and runs actions on matches.
The shell built-in that lists, searches and re-runs previous commands, and manages the history file.
A pager for viewing large files and command output one screen at a time, with search and follow mode.
Counts lines, words, characters and bytes in files or input.