The grep command reads through files line by line, checks each line against a pattern you supply, and prints every line that matches. Short for “global regular expression print,” it comes pre-installed on every Linux and Unix system and handles the majority of day-to-day text search tasks without any setup.
grep Command Syntax
Every grep call follows this layout:
grep [options] pattern [files]
options are flags that change how the search runs. pattern is the word, phrase, or regular expression to locate. files are one or more targets — grep processes all of them in sequence. To find every line in notes.txt containing “python”:
grep "python" notes.txt
How to Use grep Command on Linux: Common Options
Case-insensitive search with -i
By default, grep treats uppercase and lowercase as different characters. The -i flag removes that distinction, so grep -i "unix" geekfile.txt matches “UNIX,” “Unix,” and “unix” alike.
Count matching lines with -c
When you only need a number — not the actual lines — pass -c. The output is a single integer representing how many lines hold the pattern.
grep -c "unix" geekfile.txt
List filenames with -l
Across dozens of files, -l tells grep to print only the names of files where at least one match was found, skipping all content. You can also use the find command to locate files first, then pass results into grep via -exec for finer filtering.
grep -l "unix" f1.txt f2.txt f3.txt f4.txt
Match whole words only with -w
Without -w, grep returns a line even if the search term appears inside a larger word — searching “root” also returns “rooted.” The flag restricts results to standalone word matches.
grep -w "unix" geekfile.txt
Print only the matched portion with -o
Instead of the full line, -o outputs only the matched text, one occurrence per line. This pairs well with piping results into counting or sorting tools.
Show line numbers with -n
The -n flag prefixes each result with its line number inside the file — useful when you need to jump to an exact location in an editor or log.
grep -n "unix" geekfile.txt
Invert the match with -v
The -v flag flips the logic: grep shows every line that does not contain the pattern. Piping the output of cat into grep with -v is a quick way to strip unwanted lines from any file.
grep -v "unix" geekfile.txt
Using Regular Expressions with the grep Command in Unix
grep accepts standard regular expressions, giving precise control over matches. Two anchors cover most everyday needs:
| Symbol | Purpose | Example |
|---|---|---|
^ | Anchors pattern to line start | grep "^unix" geekfile.txt |
$ | Anchors pattern to line end | grep "os$" geekfile.txt |
You can combine anchors with character classes and quantifiers for more specific searches. When you need to locate files before scanning their contents, pairing the find utility with grep through -exec is a practical approach.
Searching Multiple Patterns with grep -e and -f
The -e flag lets you pass more than one search term in a single run. Each line that matches any of the listed patterns appears in the output.
grep -e "Agarwal" -e "Aggarwal" -e "Agrawal" geekfile.txt
For a long list of patterns, store them in a separate file — one per line — and use -f to feed it to grep. Every entry in the pattern file is treated as a distinct search term.
grep -f pattern.txt geekfile.txt
On Debian-based systems, dgrep applies the same idea directly to files belonging to installed Debian packages.
How to Show Context Lines Around grep Matches
A matching line alone sometimes tells you little. Three flags pull in surrounding content:
| Flag | Behavior |
|---|---|
-A n | Prints n lines after each match |
-B n | Prints n lines before each match |
-C n | Prints n lines both before and after each match |
For example, grep -A1 learn geekfile.txt outputs every matching line along with the one that follows it. Using tail to pull the latest entries from a log and piping them into grep with -C2 gives you the matched line plus two lines of surrounding context in a single step.
grep Command Options: Quick Reference
| Flag | Description |
|---|---|
-i | Ignore letter case |
-c | Return only the match count |
-l | Print filenames containing a hit |
-w | Match whole words only |
-o | Output only the matched portion |
-n | Prefix results with line numbers |
-v | Invert the match |
-e | Define multiple patterns |
-f | Read patterns from a file |
-A | Show lines after each match |
-B | Show lines before each match |
-C | Show lines around each match |
grep vs fgrep: When to Use Which
The standard grep command interprets patterns as regular expressions. fgrep (or grep -F) searches for fixed strings instead, making no attempt to parse regex metacharacters. For simple, literal string searches across large files, fgrep runs faster because it skips the regex engine entirely. If your search term contains characters like ., *, or [ and you want them treated as plain characters rather than patterns, fgrep is the better choice.
FAQs
What does the grep command do in Linux?
grep reads files line by line, checks each line against a given pattern, and prints every line that matches. It works on single files, multiple files, and standard input piped from other commands.
How do I use grep to search recursively through directories?
Add the -r flag: grep -r "pattern" /path/to/directory. This scans all files in the directory and its subdirectories for the specified pattern.
What is the difference between grep -i and grep -w?
-i makes the search case-insensitive, matching “Unix,” “UNIX,” and “unix.” -w restricts matches to whole words only, so “unix” won’t match “unixlike.”
How do I search for multiple patterns using the grep command?
Use -e for each pattern: grep -e "error" -e "warning" logfile.txt. Alternatively, store patterns in a file and pass it with -f pattern.txt.
Can I use grep to filter output from another command?
Yes. Pipe any command’s output into grep: ps aux | grep "apache". grep reads from standard input when no file is specified, making it a standard part of shell pipelines.