The grep command
print lines that match patterns 53 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.
Options in the examples
-E, --extended-regexp- Interpret PATTERNS as extended regular expressions (EREs, see below).
-i, --ignore-case- Ignore case distinctions in patterns and input data, so that characters that differ only in case match each other.
-r, --recursive- Read all files under each directory, recursively, following symbolic links only if they are on the command line.
-v, --invert-match- Invert the sense of matching, to select non-matching lines.
-c, --count- Suppress normal output; instead print a count of matching lines for each input file.
-l, --files-with-matches- Suppress normal output; instead print the name of each input file from which output would normally have been printed.
-A NUM, --after-context=NUM- Print NUM lines of trailing context after matching lines.
-B NUM, --before-context=NUM- Print NUM lines of leading context before matching lines.
-C NUM, -NUM, --context=NUM- Print NUM lines of output context. Places a line containing a group separator (--) between contiguous groups of matches.
-n, --line-number- Prefix each line of output with the 1-based line number within its input file.
-P, --perl-regexp- Interpret PATTERNS as Perl-compatible regular expressions (PCREs).
-w, --word-regexp- Select only those lines containing matches that form whole words.
Examples
grep 'student' /etc/passwd
grep 'error' /var/log/syslog
grep -i 'Error' file.txtCase-insensitive
grep -n 'word' file.txtShow line numbers
grep -c 'word' file.txtCount matching lines
grep -v 'word' file.txtInvert: lines that DON'T match
grep -l 'word' *.txtList files that match
grep -r 'word' directory/Recursive search
grep -w 'word' file.txtMatch whole words only
grep -A 3 'error' file.txt3 lines After match
grep -B 3 'error' file.txt3 lines Before match
grep -C 3 'error' file.txt3 lines Context (both)
grep '^root' /etc/passwdLines starting with 'root'
grep 'bash$' /etc/passwdLines ending with 'bash'
grep '[0-9]' file.txtLines containing digits
grep -E 'cat|dog' file.txtExtended regex: OR
ps aux | grep nginx
history | grep ssh
ls -la | grep '.txt'List only .txt files
cat /etc/passwd | grep '/bin/bash'Find bash users
ps aux | grep nginx | wc -lCount nginx processes
ps aux | grep -v grep | grep python
grep -r "$1" .
history | grep apt
grep 'pattern' fileBasic regex
grep -E 'pattern' fileExtended regex
grep -P 'pattern' filePerl-compatible regex
grep '^[A-Z]' fileLines starting with uppercase
grep '[0-9]\{3\}-[0-9]\{4\}' filePhone numbers
grep -E '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' emails.txt
grep -E '\b[0-9]{1,3}(\.[0-9]{1,3}){3}\b' file.txt
grep -E '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]+' file.txt
grep -E '^.{5}$' file.txt
apt list --installed | grep nginx
dpkg -l | grep nginx
dpkg -l | grep python
sudo ss -tlnp | grep :80
grep -i bot /var/log/nginx/access.log
curl -s http://app.test | grep '<p>'What do visitors see?
ps aux | grep -v grep | grep <name>
cat /etc/passwd | grep -E '/bin/(bash|sh)$'Users with shell access
cat /etc/passwd | grep ana
journalctl -u sshd | grep -i failFilter for failure mentions
grep CRON /var/log/syslogThe same story in the classic syslog
grep -r "sk_live_" /var/log/appWhich files, which lines?
grep -c "sk_live_" /var/log/app/app.logHow many times in one file?
grep -rl "sk_live_" /var/log/appJust the file names
tar -tzf /backups/FILE.tar.gz | grep websiteNarrow it down
curl -s http://localhost | grep -i titleWhat does the server answer?
journalctl -u sshd | grep mariaWhat does the server say?
sudo grep maria /var/log/auth.logThe same in the classic auth log
sudo lsof | grep deletedThe same, the long way
ps aux | grep traceThe process behind it
Lessons that use grep
- grep — Searching Text
- Pipes & Redirection
- ps — Listing Processes
- Aliases — Command Shortcuts
- Command History
- Regular Expressions
- Searching & Inspecting Packages
- dpkg — Low-Level Package Tool
- nginx — Your First Web Server
- Reading the Logs
- Project: Deploy with Rollback
- Mission: Service Down
- Mission: Security Sweep
- Mission: New Hire Setup
- Mission: Midnight Log Hunt
- Mission: The Silent Cron Job
- Mission: Leaked Credentials
- Mission: Restore From Backup
- Mission: Port 80 Is Taken
- Mission: Locked Out After the Holiday
- Mission: Deleted, But Still Full
- Mission: 403 Forbidden