Grep Command in Linux with Examples

Grep is one of the most commonly used commands in Linux. The name stands for “Global Regular Expression Print”.
The grep command searches for a text pattern in one or more files and prints every line that matches. It is a powerful tool for searching and analyzing large amounts of text data quickly and efficiently. You can use it to search for specific words, phrases, or regular expressions
in files of any size, from small configuration files to large log files. If no files are specified, grep reads from standard input, which is usually the output of another command.
This guide covers the most common GNU grep options with practical examples.
To pick the files before you search them, our find command builder
puts together the find command and its -exec or xargs handoff to grep.
grep Command Syntax
The syntax for the grep command is as follows:
grep [OPTIONS] PATTERN [FILE...]The items in square brackets are optional.
OPTIONS- Zero or more options. Grep includes a number of options that control its behavior.PATTERN- The search pattern (string or regular expression).FILE- Zero or more input file names.
To search a file, the user running the command must have read access to it.
Search for a String in Files
The most basic usage of the grep command is to search for a string (text) in a file.
For instance, to find all lines containing the string “bash” in the /etc/passwd
file, run the following command:
grep bash /etc/passwdThe output shows every line that contains the matching string:
root:x:0:0:root:/root:/bin/bash
linuxize:x:1000:1000:Linuxize:/home/linuxize:/bin/bashIf the string contains spaces, enclose it in single or double quotation marks:
grep "Gnome Display Manager" /etc/passwdCase Insensitive Search
By default, grep is case-sensitive, meaning uppercase and lowercase characters are treated as distinct.
To ignore case when searching, use the -i option (or --ignore-case). The examples in this section and the next use /usr/share/dict/words from Ubuntu’s wamerican package. You can substitute any text file if you do not have that word list.
For example, searching for “Zebra” without -i will not match lowercase entries:
grep Zebra /usr/share/dict/wordsThis word list has no entry with that exact capitalization. With -i, grep also finds the lowercase entries.
grep -i Zebra /usr/share/dict/wordszebra
zebra's
zebrasSearch for Full Words
When searching for a string, grep displays all lines where the string appears, even if it is embedded in a larger word.
For example, if you search for “gnu”, grep will match lines containing “gnu” as well as words like “Cygnus” or “wingnut”:
grep gnu /usr/share/dict/wordsCygnus
Cygnus's
gnu
gnu's
gnus
magnum
magnum's
magnums
wingnut
wingnut's
wingnuts
Xiongnu
Xiongnu'sTo return only lines where the specified string is a whole word (enclosed by non-word characters), use the -w option (or --word-regexp):
grep -w gnu /usr/share/dict/wordsgnu
gnu'sThe gnu's line still matches. The apostrophe is a non-word character, so “gnu” counts as a whole word there.
a-z, A-Z, and 0-9) and underscores (_). All other characters are considered non-word characters.Match Whole Lines
The -w option still matches a word anywhere in the line. To match the entire line, use the -x option (or --line-regexp).
This is the reliable way to test for an exact configuration value. A commented line such as #PermitRootLogin yes will not match:
grep -x "PermitRootLogin yes" /etc/ssh/sshd_configFor a regular expression, -x behaves as if you had wrapped the pattern in parentheses and surrounded it with ^ and $.
Invert Match (Exclude)
Use the -v option (or --invert-match) to display lines that do not match a pattern.
For example, to print lines that do not contain the string “nologin”:
grep -v nologin /etc/passwdroot:x:0:0:root:/root:/bin/bash
sync:x:4:65534:sync:/bin:/bin/sync
dhcpcd:x:996:996:DHCP Client Daemon:/usr/lib/dhcpcd:/bin/false
linuxize:x:1000:1000:Linuxize:/home/linuxize:/bin/bash
whoopsie:x:103:111::/nonexistent:/bin/false
speech-dispatcher:x:106:29:Speech Dispatcher:/run/speech-dispatcher:/bin/false
hplip:x:112:7:HPLIP system user:/run/hplip:/bin/false
gdm:x:974:974:Gnome Display Manager:/var/lib/gdm3:/bin/falseThe remaining lines belong to accounts with a login shell such as /bin/bash, plus system accounts that use /bin/false or /bin/sync instead of nologin.
Recursive Search
To recursively search for a pattern, invoke grep with the -r option (or --recursive). When this option is used, grep searches through all files in the specified directory and its subdirectories, skipping symlinks that are encountered recursively.
To follow all symbolic links
, use the -R option (or --dereference-recursive) instead.
Here is an example showing how to search for the string linuxize.com in all files inside the /etc directory:
grep -r linuxize.com /etcThe output includes matching lines prefixed by the full path to the file:
/etc/hosts:127.0.0.1 node2.linuxize.com
/etc/nginx/sites-available/linuxize.com: server_name linuxize.com www.linuxize.com;If you use the -R option, grep will follow all symbolic links:
grep -R linuxize.com /etcNotice the last line in the output below. It is not printed when grep is invoked with -r because files inside Nginx’s sites-enabled directory are symlinks to configuration files inside sites-available:
/etc/hosts:127.0.0.1 node2.linuxize.com
/etc/nginx/sites-available/linuxize.com: server_name linuxize.com www.linuxize.com;
/etc/nginx/sites-enabled/linuxize.com: server_name linuxize.com www.linuxize.com;For faster recursive searches in code repositories, ripgrep
is often a better default because it respects ignore files and searches recursively without extra flags.
Show Only the Filename
To suppress the default grep output and print only the names of files containing a match, use the -l option (or --files-with-matches).
The following command searches for all files ending with .conf in the current working directory
and displays only the file names:
grep -l linuxize.com *.conftmux.conf
haproxy.confThe -l option is commonly used together with the recursive option -R:
grep -Rl linuxize.com /tmpThe opposite option is -L (or --files-without-match). It prints the name of every input file that produced no match. Use it for audits, such as finding configuration files that are missing a required directive:
grep -L "PermitRootLogin" /etc/ssh/sshd_config.d/*.confShow Line Numbers
The -n option (or --line-number) tells grep to prefix each matching line with its line number in the file.
For instance, to display matching lines from /etc/services containing the string 10000 with their line numbers:
grep -n 10000 /etc/servicesThe line number depends on your distribution’s /etc/services file. On Ubuntu 26.04, the match is on line 349:
349:webmin 10000/tcpCombined with -r, the -n option shows exactly where each match lives across a directory tree:
grep -rn "GRUB_TIMEOUT" /etc/default/Each line shows the file path, the line number, and the matching text, separated by colons:
/etc/default/grub.d/50-cloudimg-settings.cfg:8:GRUB_TIMEOUT=0
/etc/default/grub:7:GRUB_TIMEOUT_STYLE=hidden
/etc/default/grub:8:GRUB_TIMEOUT=0The setting appears in two files, and the drop-in under grub.d is easy to miss when you open only /etc/default/grub. The pattern also matches GRUB_TIMEOUT_STYLE. Add -w to skip it.
Count Matches
To print only the number of matching lines instead of the lines themselves, use the -c option (or --count).
In the following example, we count the number of accounts that have /usr/bin/zsh as their shell:
grep -c '/usr/bin/zsh' /etc/passwd4Print Only the Matching Part
The -o option (or --only-matching) tells grep to print only the matched portion of each line, rather than the entire line. This is especially useful when extracting specific patterns with regular expressions.
To print the first five words beginning with “log”, pipe the matches to head -n 5.
grep -o '\blog\w*' /var/log/syslog | head -n 5The \b anchor starts the match at a word boundary, so syslog is skipped. The \w* matches any letters, digits, or underscores after log, keeping words such as logFile and log2 intact.
/var/log/syslog, where rsyslog typically writes on Ubuntu. On RHEL-based distributions with rsyslog, look for /var/log/messages. The Ubuntu 26.04 Minimal cloud image and default installations of Debian 12 or newer use the systemd journal. If you do not have a log file to search, try journalctl | grep -i "error". See the journalctl guide
for more ways to filter the journal.Highlight Matches with Color
The --color option surrounds each match with terminal escape sequences, so the matching text stands out from the rest of the line. Use the value auto in most cases. It enables color when the output goes to a terminal and disables it when the output is piped or redirected:
grep --color=auto "error" /var/log/syslogMost distributions already alias grep to grep --color=auto. That is why matches appear highlighted without the option. To keep color through a pipe, use --color=always. The escape sequences then become part of the text that the next command reads.
Context Lines
By default, grep prints only the lines that match. You can use context options to also display lines surrounding each match.
To print a specific number of lines before each match, use the -B option (or --before-context):
grep -B 3 "error" /var/log/syslogTo print a specific number of lines after each match, use the -A option (or --after-context):
grep -A 3 "error" /var/log/syslogTo print lines both before and after each match, use the -C option (or --context):
grep -C 3 "error" /var/log/syslogWhen the context lines of two matches touch or overlap, grep prints them as one group. Groups that are farther apart are separated by a line containing --:
seq 1 10 | grep -A 1 -e 2 -e 3 -e 82
3
4
--
8
9The matches on 2 and 3 share one group, and the match on 8 starts a new group after the separator.
You can also pass the number as an option on its own. grep -2 "error" /var/log/syslog is the same as grep -C 2 "error" /var/log/syslog.
Using Grep to Filter Command Output
A command’s output can be filtered with grep through piping, and only lines matching a given pattern will be printed. This is one of the most common uses of grep.
For instance, to find which processes are running as user www-data, you can pipe the output of the ps
command to grep:
ps -ef | grep www-datawww-data 18247 12675 4 16:00 ? 00:00:00 php-fpm: pool www
www-data 31147 12770 0 Oct22 ? 00:05:51 nginx: worker process
www-data 31148 12770 0 Oct22 ? 00:00:00 nginx: cache manager processNote that grep may also match its own process in the output. A common trick to avoid this is to use a character class in the pattern:
ps -ef | grep '[w]ww-data'By wrapping the first character in brackets, the grep command itself no longer matches the pattern.
Filtering a Live Stream
Filtering a continuous stream, such as a log file followed with tail -f, works the same way:
tail -f /var/log/nginx/error.log | grep "upstream"Matching lines appear on your screen as soon as they are written. When grep writes to a terminal, it flushes each line right away.
That changes when you pipe the result into another command or redirect it to a file. grep then buffers its output in large blocks, and matching lines can sit in the buffer for a long time before the next command sees them. Add the --line-buffered option to flush every matching line as soon as it is found:
tail -f /var/log/nginx/error.log | grep --line-buffered "upstream" | tee -a upstream.logtee -a displays each match and appends it to upstream.log, preserving earlier matches if you restart the command.
Search for Multiple Strings (Patterns)
Two or more search patterns can be joined using the OR operator |.
By default, grep interprets the pattern as a basic regular expression, where | must be escaped with a backslash:
grep 'fatal\|error\|critical' /var/log/nginx/error.logIf you use the extended regular expression option -E, the operator does not need to be escaped:
grep -E 'fatal|error|critical' /var/log/nginx/error.logFor more complex multi-pattern searches, see Grep Multiple Patterns .
Basic Regular Expressions
GNU Grep has three regular expression feature sets: Basic, Extended, and Perl-compatible.
By default, grep interprets the pattern as a basic regular expression (BRE), where all characters except the meta-characters match themselves.
Below is a list of the most commonly used meta-characters:
^- Matches the start of a line. In the following example,kangaroomatches only if it occurs at the beginning of a line:Terminalgrep "^kangaroo" file.txt$- Matches the end of a line. Here,kangaroomatches only if it occurs at the end of a line:Terminalgrep "kangaroo$" file.txt.- Matches any single character. To match anything that begins withkan, then has two characters, and ends withroo:Terminalgrep "kan..roo" file.txt[ ]- Matches any single character enclosed in the brackets. For instance, to find lines that containacceptoraccent:Terminalgrep "acce[np]t" file.txt[^ ]- Matches any single character not enclosed in the brackets. The following pattern matches strings likecocaorcoma, but notcola:Terminalgrep "co[^l]a" file.txt
To escape the special meaning of a meta-character, use the \ (backslash) symbol.
Extended Regular Expressions
To interpret the pattern as an extended regular expression (ERE), use the -E option (or --extended-regexp). Extended regular expressions include all basic meta-characters plus additional ones such as +, ?, |, and ().
To extract common email addresses from a file, combine -E with -o.
grep -E -o '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b' file.txtThe -o option prints each matching address on its own line. The final [A-Za-z]{2,} matches domain endings such as .com and .technology.
If you have a file with one IPv4 address per line, use -x to keep only lines that match the full address pattern.
grep -Ex '((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])' file.txtThe pattern checks that all four numbers are between 0 and 255, with no leading zeros. Since -x checks the entire line, 999.1.2.3 is rejected.
Perl-Compatible Regular Expressions
The third feature set is enabled with the -P option (or --perl-regexp). It interprets the pattern as a Perl-compatible regular expression and adds constructs such as \d, \t, lookarounds, and non-greedy quantifiers. GNU basic and extended regular expressions already recognize \w, \W, \s, and \S as special backslash expressions.
For example, to find lines where a number is preceded by a dollar sign, without printing the dollar sign itself:
grep -Po '(?<=\$)\d+' invoice.txtThe -P option is a GNU extension. It is missing from some builds, including the BSD grep shipped with macOS. For the full syntax of all three feature sets, see the guide to regular expressions in grep
.
Search Only Specific File Types
The --include option is the counterpart of --exclude. Instead of listing what to skip, you state which file names to search. Combined with -r, it is the shortest way to search one file type across a project.
To search only Python files for the string “requests”:
grep -r --include="*.py" "requests" .Quote the glob so the shell passes it to grep unchanged. In zsh, the default shell on macOS, an unquoted --include=*.py stops with zsh: no matches found: --include=*.py. Repeat the option to cover several types:
grep -r --include="*.py" --include="*.cfg" "requests" .Excluding Files and Directories
When searching recursively, you can exclude specific files or directories using --exclude and --exclude-dir.
To exclude all .log files from a recursive search:
grep -r --exclude="*.log" "pattern" /path/to/directoryTo exclude multiple file types, repeat the --exclude option:
grep -r --exclude="*.log" --exclude="*.tmp" "pattern" /path/to/directoryTo exclude a directory from the search:
grep -r --exclude-dir="node_modules" "pattern" /path/to/directoryFor more details and examples, see Grep Exclude .
Quiet Mode
The -q option (or --quiet) tells grep to run silently without displaying anything on standard output. If a match is found, the command exits with status 0. This is useful in shell scripts where you want to check whether a file contains a string and perform an action based on the result.
When searching a single file, use the exit status to distinguish a match, a miss, and a read error in an if statement
.
if grep -q PATTERN filename
then
echo pattern found
elif [ "$?" -eq 1 ]
then
echo pattern not found
else
exit 2
fiStatus 1 means the file contained no match. If the file is missing or unreadable, grep prints an error to standard error and the script exits with status 2.
Using Shell Variables with Grep
When using grep in shell scripts, you can pass shell variables as the search pattern. Enclose the variable in double quotes to prevent word splitting and globbing:
pattern="search term"
grep "$pattern" file.txtTo use a variable as a fixed string (disabling regex interpretation), combine it with the -F option:
grep -F "$pattern" file.txtLimit the Number of Matches
The -m option (or --max-count) tells grep to stop reading a file after a specified number of matching lines. This is useful when working with large files and you only need the first few matches.
For example, to print only the first five lines containing “error” in a log file:
grep -m 5 "error" /var/log/syslogGrep stops reading the file as soon as it finds five matches, which can save a lot of time on large files.
Suppress Error Messages
By default, grep prints error messages when it cannot read a file or when a file does not exist. The -s option (or --no-messages) silences these messages.
This is helpful in shell scripts where you want to ignore “No such file or directory” or “Permission denied” warnings:
grep -s "pattern" /etc/*.confAny files that cannot be read are skipped silently, and only the actual matches are printed.
Hide Filenames in Output
When grep searches multiple files, it prefixes each matching line with the file name. The -h option (or --no-filename) suppresses these prefixes and prints only the matching lines.
For example, to search for “PermitRootLogin” across all SSH configuration files without showing file names:
grep -h "PermitRootLogin" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*.confThis is useful when piping the output to another command or when the file names would only add noise to the result.
Search Binary Files as Text
Grep treats binary files differently from text files. When a binary file matches, it prints a message such as grep: archive.dat: binary file matches to standard error instead of the actual content. Releases older than GNU grep 3.5 printed Binary file archive.dat matches to standard output. The -a option (or --text) forces grep to process binary files as if they were text.
To search for a string in a binary file or a data file that contains non-text bytes:
grep -a "version" archive.datThe matching lines are printed even though the file may contain non-printable characters. Be aware that the output may include control characters that affect your terminal.
Search Compressed Files
Rotated log files are usually compressed. A plain grep over /var/log/syslog* silently misses everything in the .gz archives. The zgrep wrapper decompresses each file on the fly and passes the result to grep. It accepts most of the same options:
zgrep -c "error" /var/log/syslog.2.gzIt also handles a mix of compressed and plain files in one command. That is what you need when a search spans a whole rotation cycle:
zgrep -i "out of memory" /var/log/syslog*Companion wrappers exist for other formats, including bzgrep for bzip2 and xzgrep for xz archives.
Recursive search is the exception. zgrep -r fails with /usr/bin/zgrep: -r: option not supported, so pair find with zgrep when the archives are spread across directories.
Combining Grep with find
When you need more control over which files are searched, combine grep with the find command using -exec. This is helpful when you want to filter by file attributes such as modification time, size, or permissions before searching their contents.
For example, to search for “TODO” in all .py files modified in the last seven days:
find . -type f -name "*.py" -mtime -7 -exec grep -l "TODO" {} +The find command locates the matching files, and grep -l prints the names of files that contain the pattern. Using + instead of \; passes files to grep in batches, which is much faster than invoking grep once for each file.
Troubleshooting
\d matches the letter d instead of digits
The \d shorthand belongs to Perl-compatible regular expressions. In basic and extended patterns, a backslash before an ordinary character has unspecified behavior, and current GNU grep reads \d as a plain “d”. Upstream releases since 3.8 also print grep: warning: stray \ before d, as RHEL-based systems do. Debian and Ubuntu patch the warning out, so you get no hint that the pattern is wrong. Use a bracket expression that works everywhere, grep -E '[0-9]+' or grep -E '[[:digit:]]+', or switch the engine with grep -P '\d+'.
A tab in the pattern does not match
The \t escape is also PCRE syntax. Outside PCRE, GNU grep reads it as the letter “t”, so grep '\t' matches every line that contains a “t”. Upstream builds warn with stray \ before t. Use grep -P '\t', or let the shell insert a real tab with grep $'\t' file.txt.
grep prints “binary file matches”
Grep classified the file as binary, often because it found null bytes or encountered encoding errors. It prints grep: FILE: binary file matches to standard error and suppresses the matching content to avoid sending binary data to your terminal. Pass -a to treat the file as text, or -I to skip binary files entirely during a recursive search. Log files often trigger this after a partial write. Check the file before assuming the message is wrong.
A pattern that starts with a dash is read as an option
Grep parses grep -v- file.txt as options. Mark the pattern explicitly with -e, as in grep -e "-v-" file.txt, or end option parsing with --, as in grep -- "-v-" file.txt.
The search matches unexpected files or nothing at all
The shell expands an unquoted pattern before grep runs. A command such as grep *.conf /etc/hosts can turn into something completely different. Quote every pattern that contains *, ?, [, $, or spaces.
Recursive searches fill the screen with “Permission denied”
Those messages come from directories your user cannot read. Add the -s option to silence them, or run the search through sudo when you genuinely need the protected paths.
Patterns anchored with $ fail on files from Windows
Files with CRLF line endings keep a carriage return at the end of every line. The line really ends with value\r, so grep "value$" never matches. Confirm the line endings with file data.txt. Then convert the file with dos2unix, or allow for the extra character with grep -P "value\r?$".
Quick Reference
For a printable quick reference, see the Grep cheatsheet .
| Task | Command |
|---|---|
| Search for a string | grep "pattern" file |
| Case insensitive search | grep -i "pattern" file |
| Whole word match | grep -w "word" file |
| Whole line match | grep -x "whole line" file |
| Invert match | grep -v "pattern" file |
| Recursive search | grep -r "pattern" /path |
| Show filenames only | grep -rl "pattern" /path |
| Show files without a match | grep -L "pattern" file1 file2 |
| Show line numbers | grep -n "pattern" file |
| Count matches | grep -c "pattern" file |
| Print only matching part | grep -o "pattern" file |
| Lines before match | grep -B 3 "pattern" file |
| Lines after match | grep -A 3 "pattern" file |
| Lines around match | grep -C 3 "pattern" file |
| Multiple patterns | grep -e "pat1" -e "pat2" file |
| Fixed string (no regex) | grep -F "literal.string" file |
| Perl-compatible regex | grep -P '\d+' file |
| Search one file type | grep -r --include="*.py" "pattern" /path |
| Exclude files | grep -r --exclude="*.log" "pattern" /path |
| Exclude directories | grep -r --exclude-dir=".git" "pattern" /path |
| Recursive with line numbers | grep -rn "pattern" /path |
| Hide filenames | grep -h "pattern" file1 file2 |
| Highlight matches | grep --color=auto "pattern" file |
| Limit matches | grep -m 5 "pattern" file |
| Suppress errors | grep -s "pattern" file |
| Search binary as text | grep -a "pattern" binary |
| Search compressed logs | zgrep "pattern" /var/log/syslog* |
| Filter a live stream | tail -f file | grep "pattern" |
| Find and grep | find . -name "*.py" -exec grep -l "pattern" {} + |
FAQ
What does grep do?
The grep command searches text for lines that match a pattern. It reads input from files or standard input, applies a search pattern (either a literal string or a regular expression), and prints every matching line. It is one of the most-used tools for filtering log files, extracting data from command output, and finding code in projects.
What does grep mean?
The name stands for “Global Regular Expression Print”. It comes from the ed text editor command g/re/p, which globally searches for a regular expression and prints matching lines. Grep became a standalone Unix utility in 1973 and has shipped with every major Unix-like system since.
What is the difference between grep, egrep, and fgrep?
They select different pattern engines. The egrep command is equivalent to grep -E. It reads the pattern as an extended regular expression, so meta-characters like +, ?, |, and () do not need to be escaped. The fgrep command is equivalent to grep -F. It treats the pattern as a fixed string, so a character such as . matches only a literal dot. Both are deprecated. Upstream GNU grep 3.8 and newer print a warning such as egrep: warning: egrep is obsolescent; using grep -E. RHEL-based systems show it. Debian and Ubuntu remove the warning, but the commands are still deprecated there. Use grep -E and grep -F instead.
How do I search for a string in all files in a directory?
Use the -r (recursive) option: grep -r "pattern" /path/to/directory. This searches all files in the directory and its subdirectories. Add --include="*.txt" to limit the search to specific file types.
How do I search for multiple words with grep?
Use the -E option with the alternation operator: grep -E "word1|word2|word3" file. Without -E, escape the pipe characters: grep "word1\|word2\|word3" file.
What is the difference between grep, sed, and awk?
Grep is designed for searching and filtering lines that match a pattern. Sed is a stream editor for transforming text with find-and-replace operations. Awk is a full programming language for field-based text processing and data extraction. Use grep to find lines, sed to modify text, and awk to process columnar data.
Conclusion
For everyday work, grep -rn with --include covers most searches through code and configuration, and -E handles alternation without backslashes. When your patterns grow beyond a few meta-characters, the guide to regular expressions in grep
explains each syntax in detail.
For more information, see the official Grep manual .
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page