grep Basics

You're debugging a production issue. There's a 500MB log file. You need to find all errors.

You could open it in a text editor and search. Or you could use grep and have the answer in 0.1 seconds.

What is grep?

grep searches text for patterns and prints matching lines.

The name comes from the ed editor command g/re/p - "globally search for regular expression and print."

Basic Usage

Terminal
$grep 'error' application.log
[ERROR] Database connection failed [ERROR] User authentication timeout [ERROR] Payment processing error

Every line containing "error" is printed.

Search Multiple Files

Terminal
$grep 'TODO' *.js
app.js:// TODO: Add error handling utils.js:// TODO: Refactor this function server.js:// TODO: Implement caching

With multiple files, grep shows the filename before each match.

Search all files in a directory tree:

Terminal
$grep -r 'password' /etc/
/etc/nginx/conf.d/site.conf:# password protected /etc/ssh/sshd_config:PasswordAuthentication yes

Use -r Liberally

When you're not sure where something is, grep -r 'term' . searches the entire current directory tree.

Terminal
$grep 'error' log.txt
(no matches)
$grep -i 'error' log.txt
ERROR: Something failed Error: Invalid input error: timeout

-i matches regardless of case.

Show Line Numbers

Terminal
$grep -n 'function' script.js
15:function handleClick() { 42:function validateInput() { 89:function submitForm() {

-n shows which line the match is on.

Count Matches

Terminal
$grep -c 'ERROR' application.log
47

-c counts matching lines instead of printing them.

Invert Match

Find lines that DON'T match:

Terminal
$grep -v 'DEBUG' application.log
(all lines except DEBUG messages)

-v is incredibly useful for filtering out noise.

Terminal
$# Show all errors, excluding known issues:
$grep 'ERROR' log.txt | grep -v 'known_issue'
(errors except known issues)

Only Print Filenames

Terminal
$grep -l 'config' *.json
app.config.json package.json tsconfig.json

-l lists files that contain the pattern, without showing the matches.

Opposite: -L lists files that DON'T contain the pattern.

Combining Flags

Flags can be combined:

Terminal
$grep -rni 'todo' .
./src/app.js:15:// TODO: fix this ./README.md:42:TODO: update docs

-rni = recursive + line numbers + case-insensitive

Real-World Examples

Find Errors in Logs

Terminal
$grep -i 'error\|fail\|fatal' /var/log/syslog
(all error-related lines)

Search Code for Functions

Terminal
$grep -rn 'def ' *.py
(all Python function definitions)

Check Config for Setting

Terminal
$grep -r 'port' /etc/nginx/
(find port configurations)
Knowledge Check

How do you find all lines that do NOT contain 'DEBUG' in a log file?

Modern Alternative: ripgrep (rg)

ripgrep is a faster, more user-friendly grep replacement:

hljs bash
sudo apt install ripgrep  # Then use: rg
Terminal
$rg 'TODO' --type js
(searches .js files, respects .gitignore)
$rg -i 'error' .
(recursive by default, case-insensitive)

ripgrep is:

  • Faster - significantly faster on large codebases
  • Smarter - ignores .git, node_modules by default
  • Prettier - colored output with context

For Interactive Use

Use rg for daily work - it's faster and has sensible defaults. Use grep in scripts for portability (grep is everywhere, rg might not be installed).

Quick Reference

FlagEffect
-iCase-insensitive
-rRecursive (search directories)
-nShow line numbers
-cCount matches
-vInvert (show non-matches)
-lList only filenames
-LList files without matches

Key Takeaways

  • grep 'pattern' file searches for text
  • -i for case-insensitive
  • -r for recursive directory search
  • -n for line numbers
  • -v to invert (exclude matches)
  • Combine flags: grep -rni 'term' .

Next: advanced grep with regex and more flags.