Knowledgebase

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

The Basic Search

grep "upstream timed out" /var/log/nginx/error.log

grep reads the file line by line and prints every line that contains the text. The search is case-sensitive: "error" does not match "Error". Quote the pattern whenever it contains spaces or characters the shell treats specially, such as *, $, or |. Single quotes are the safest, because the shell leaves everything inside them alone.

grep is its own GNU project and is the same program on Ubuntu, Debian, AlmaLinux, and Rocky Linux, so every option in this guide works on all of them.

The Options You Use Most

OptionWhat it does
-iIgnores case
-nShows line numbers
-vShows lines that do not match
-cCounts matching lines
-lLists only file names
-wMatches whole words only
-rSearches a folder tree
-oPrints only the matched part
-qPrints nothing, sets the exit status
-FTreats the pattern as plain text

Short options combine: grep -in "timeout" app.log ignores case and shows line numbers.

Search a Whole Folder

grep -rn "server_name" /etc/nginx/
grep -rl "DB_HOST" /var/www/
grep -rn --include="*.conf" "listen" /etc/
grep -rn --exclude-dir=node_modules "API_URL" ~/app

-r searches every file below the folder but skips symbolic links it meets inside the tree; -R follows all of them. --include limits the search to file names that match a pattern; quote it so the shell does not expand it first. --exclude-dir skips folders such as dependency or cache directories, which saves a lot of time on application trees. To pick files by size, age, or owner before searching them, combine grep with the find command.

Show Context Around a Match

grep -A 3 "Out of memory" app.log
grep -B 2 "segfault" app.log
grep -C 5 "upstream timed out" /var/log/nginx/error.log

-A prints lines after each match, -B lines before it, and -C both. Groups are separated by a line with two dashes. Context is what turns a single error line into an explanation: the lines before it often show the request or job that caused it.

Whole Words and Regular Expressions

grep -w "root" /etc/passwd
grep -E "^(GET|POST) " requests.log
grep -E " (404|500) " /var/log/nginx/access.log
grep -F "[error]" app.log

-w finds "root" but not "chroot". By default grep uses basic regular expressions; -E switches to extended ones, so |, +, ?, and parentheses work without backslashes. ^ anchors the start of a line and $ the end. -F turns all of that off, so brackets and dots are matched literally. To search for several words at once, as OR or AND, see how to grep multiple strings.

Filter the Output of Other Commands

ss -tlnp | grep ":443"
dpkg -l | grep -i php
journalctl -u nginx --since today | grep -i error
tail -f /var/log/nginx/access.log | grep --line-buffered " 500 " | awk '{print $1}'

Any command that prints text can feed grep. When grep's output goes into another pipe instead of the screen, add --line-buffered so each line is passed on at once; otherwise the last command in a live pipeline seems to hang. Following logs is covered in the tail command guide, and the socket list in the ss command guide.

Counting and the Exit Status

grep -c " 404 " /var/log/nginx/access.log
grep -o "wp-login" /var/log/nginx/access.log | wc -l

-c counts lines, not occurrences; -o with wc -l counts every occurrence. grep exits with 0 when it finds a match, 1 when it finds none, and 2 on an error, which makes it a test in scripts:

if grep -q "^PasswordAuthentication no" /etc/ssh/sshd_config; then
  echo "Password login is off"
fi

More on tests is in the Bash if statement guide.

Common Problems

  • Binary file matches: grep found the text in a file it considers binary. Add -a to treat it as text, or narrow the search with --include.
  • Permission denied: logs under /var/log are often readable only by root. Run grep with sudo, or add -s to hide messages about files you cannot read.
  • A pattern that starts with a dash: grep reads it as an option. Write grep -e "-v" file or grep -- "-v" file.
  • The grep process shows up in ps output: use pgrep -a nginx instead of piping ps into grep.

To change the text you found, use sed; to pull out single columns, use awk.

Was this answer helpful?
Loading...