Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Grep for Several Strings

grep -e error -e warning app.log
grep -E 'error|warning' app.log
grep 'error\|warning' app.log
grep error app.log | grep timeout

The first three lines print every line that contains error or warning. The last prints only lines that contain both error and timeout. grep on Ubuntu, Debian, AlmaLinux, and Rocky Linux is GNU grep, so all four forms work on each of them.

Match Any of Several Strings (OR)

  • Repeated -e: each -e adds one pattern. This is the clearest form and needs no special characters.
  • -E 'a|b|c': extended regular expressions use | for "or". Quote the pattern so the shell does not read | as a pipe.
  • 'a\|b': the same in basic syntax, which GNU grep understands.
  • -f patterns.txt: reads one pattern per line from a file, for long lists.

Match Lines That Contain All Strings (AND)

grep error app.log | grep timeout
grep -E 'error.*timeout|timeout.*error' app.log
grep error app.log | grep timeout | grep -v debug

Piping one grep into another is the simplest AND: the second grep sees only the lines the first one kept. The single -E pattern does the same in one command and covers both orders. grep -v at the end drops lines that contain a word. For files that contain both strings anywhere, not on the same line, list the files with grep -l error *.log first, then search those files for the second string.

Fixed Strings, Case, and Whole Words

OptionEffect
-FPatterns are plain text
-iIgnores case
-wWhole words only
-xWhole lines only
grep -F -e '203.0.113.10' -e '[warn]' access.log
grep -iw -e fail -e denied app.log

Without -F, a dot matches any character and brackets start a character set, so 203.0.113.10 could also match other addresses and [warn] would match any line with a w, a, r, or n. -w stops fail from matching failed, and -i also finds Fail and FAIL.

Search Folders Recursively

grep -rn -e listen -e server_name /etc/nginx
grep -rn --include='*.conf' -e listen -e server_name /etc/nginx
grep -rn --exclude-dir=vendor -e 'DB_HOST' -e 'DB_NAME' /var/www/app

-r searches every file below the folder, and -n adds line numbers. --include limits the search to file names that match a pattern, and --exclude-dir skips folders such as vendor or node_modules. To pick files by age or size as well, combine grep with find.

Count Matches and List Files

grep -c -e error -e warning app.log
grep -rl -e error -e warning /var/log/app
grep -rL 'require_once' /var/www/app
grep -oE 'error|warning' app.log | sort | uniq -c

-c counts matching lines, not matches, and gives one total for all the patterns together. -l prints only the names of files that match, and -L the names of files that do not. The last line counts each string separately: -o prints every match on its own line, and sort with uniq -c counts them.

Show Context Around a Match

grep -C 3 -e error -e fatal app.log

-A 3 prints three lines after each match, -B 3 three lines before, and -C 3 both, which often shows the cause of an error.

Exit Codes and Compressed Logs

grep exits with 0 when it finds a match, 1 when it finds none, and 2 on an error such as a missing file. Scripts use this in tests, as described in the bash if statement guide: if grep -q -e error -e fatal app.log; then, where -q prints nothing. For rotated logs that end in .gz, zgrep takes the same options, except the recursive ones such as -r and --include. To watch a log for new matches as they arrive, see the tail command guide.

Was this answer helpful?
Loading...