TExcelTAD
Home › Linux & Bash › grep: Search Text

grep: Search Text

grep prints the lines of its input that match a pattern. It is the tool for questions like "which log lines mention ERROR?", "where in my code do I call connect()?" or "which config files set a port?".

Basic search

grep PATTERN FILE... prints every matching line:

bash · try it yourself
cat > app.log <<'LOG'
2026-03-01 08:00:01 INFO  server started on port 8080
2026-03-01 08:02:13 WARN  slow query: 1.8s
2026-03-01 08:05:40 ERROR database connection lost
2026-03-01 08:05:41 INFO  retrying connection
2026-03-01 08:05:42 ERROR retry failed
LOG
grep ERROR app.log
2026-03-01 08:05:40 ERROR database connection lost
2026-03-01 08:05:42 ERROR retry failed

The search is case-sensitive and matches anywhere in the line: grep connect would match connection too.

The most useful options

Option Effect
-i Ignore case
-v Invert: print lines that do not match
-n Show line numbers
-c Only count matching lines
-w Match whole words only
-x Match whole lines only
-o Print only the matching part
-l Only list names of files that match
-r Search directories recursively
-A n / -B n / -C n Show n lines after / before / around each match
-F Treat the pattern as fixed text, not a regular expression
-E Extended regular expressions (+, ?, |, () work without backslashes)
bash · try it yourself
cat > app.log <<'LOG'
2026-03-01 08:00:01 INFO  server started on port 8080
2026-03-01 08:02:13 WARN  slow query: 1.8s
2026-03-01 08:05:40 ERROR database connection lost
2026-03-01 08:05:41 INFO  retrying connection
2026-03-01 08:05:42 ERROR retry failed
LOG
grep -c ERROR app.log
grep -n -i "connection" app.log
grep -v INFO app.log
2
3:2026-03-01 08:05:40 ERROR database connection lost
4:2026-03-01 08:05:41 INFO  retrying connection
2026-03-01 08:02:13 WARN  slow query: 1.8s
2026-03-01 08:05:40 ERROR database connection lost
2026-03-01 08:05:42 ERROR retry failed

Context around a match

When a log line tells you something failed, the lines just before it often tell you why:

bash · try it yourself
cat > deploy.log <<'LOG'
step 1: pull image
step 2: run migrations
  migration 0042 applied
  migration 0043: column "email" already exists
FAILED
step 3: restart service
LOG
grep -B 2 FAILED deploy.log
echo "---"
grep -A 1 "step 2" deploy.log
  migration 0042 applied
  migration 0043: column "email" already exists
FAILED
---
step 2: run migrations
  migration 0042 applied

Searching many files

bash · try it yourself
mkdir -p src
printf 'import db\ndb.connect()\n' > src/main.py
printf 'def connect():\n    pass\n' > src/db.py
printf 'print("hello")\n' > src/hello.py
grep -r "connect" src | sort
echo "---"
grep -rl "connect" src | sort
echo "---"
grep -rn --include='*.py' "def " src
src/db.py:def connect():
src/main.py:db.connect()
---
src/db.py
src/main.py
---
src/db.py:1:def connect():

With several files, each line is prefixed with the file name. -l lists just the files, perfect as input for another command.

Whole words and only the match

bash · try it yourself
printf 'cat\ncatalog\nconcatenate\nthe cat sat\n' > words.txt
grep -w cat words.txt
echo "---"
printf 'user=ada id=17\nuser=linus id=4\n' > users.txt
grep -o 'id=[0-9]*' users.txt
grep -oE '[0-9]+' users.txt | paste -sd+ | bc
cat
the cat sat
---
id=17
id=4
21

-o extracts just the matching text, one match per line. The last line adds up the numbers: paste -sd+ joins them with + and bc calculates.

Exit status: grep in conditions

grep exits with 0 if it found something, 1 if not and 2 on errors. Add -q (quiet) to print nothing, which makes it a perfect test in scripts:

bash · try it yourself
printf 'PermitRootLogin no\nPort 22\n' > sshd_config
if grep -q '^PermitRootLogin no' sshd_config; then
  echo "OK: root login disabled"
fi
grep -q 'PasswordAuthentication' sshd_config || echo "WARN: PasswordAuthentication not set"
grep missing nofile.txt
echo "status: $?"
OK: root login disabled
WARN: PasswordAuthentication not set
grep: nofile.txt: No such file or directory
status: 2

^ in the pattern means "at the start of the line". Patterns are regular expressions; the Regex lesson covers them properly. When your search text contains characters like . or * and you mean them literally, use -F or escape them.

Common mistakes

  • Forgetting quotes: grep error message log.txt searches for error in two files named message and log.txt. Write grep "error message" log.txt.
  • grep -r pattern with no directory searches the current directory in GNU grep, but older greps wait for keyboard input. Pass . explicitly.

Test yourself

Which option prints lines that do NOT match?

  1. -n
  2. -v
  3. -x
  4. -o

What does grep -c ERROR app.log print?

  1. The matching lines
  2. The number of matching lines
  3. The line numbers
  4. The file name

Which exit status does grep return when nothing matches?

  1. 0
  2. 1
  3. 2
  4. 127