grep is the standard Unix text-search command for finding lines that match a pattern in one or more files. This guide helps DevOps engineers, sysadmins, and shell users quickly choose the right grep options and write safer, more predictable search commands.
Originally published on 2010-04-16; fully revised and updated.

grep syntax at a glance
The base form is pattern-first, then one or more files. If no file is provided, grep reads from standard input.
grep [options] 'pattern' file1 [file2 ...]
# stdin example
journalctl -u sshd | grep -i 'failed'Code language: PHP (php)
Most useful grep options (quick comparison)
| Option | What it does | Example |
|---|---|---|
| -i | Ignore case during matching | grep -i ‘agarwal’ emp.lst |
| -v | Invert match (show lines that do not match) | grep -v ‘director’ emp.lst |
| -n | Show line numbers with matched lines | grep -n ‘marketing’ emp.lst |
| -c | Count matching lines (not total match occurrences) | grep -c ‘director’ emp.lst |
| -l | List only filenames that contain at least one match | grep -l ‘manager’ *.lst |
| -e | Add multiple patterns on the command line | grep -e ‘Agarwal’ -e ‘aggarwal’ emp.lst |
| -f | Read patterns from a file (one pattern per line) | grep -f pattern.lst emp.lst |
How grep evaluates a search request
flowchart TD
A[Choose input files or stdin] --> B[Choose pattern source]
B --> C[Inline pattern]
B --> D[Pattern file with -f]
C --> E[Apply options like -i -v -n]
D --> E
E --> F{Any matching lines}
F -- yes --> G[Print selected output]
F -- no --> H[Return nonzero status]
This flow is useful in scripts: grep output gives you the matching lines, and grep exit status can drive conditional logic.
Practical command examples
# Basic single-file search
grep 'sales' emp.lst
# Search across multiple files
grep 'director' emp1.lst emp2.lst
# Phrase search (quoted pattern)
grep 'jai sharma' emp.lst
# Variable expansion inside double quotes
grep "jai Sharma $var" emp.lst
# Ignore case
grep -i 'agarwal' emp.lst
# Exclude matching lines and write remaining lines to a new file
grep -v 'director' emp.lst > other.list
# Show line numbers
grep -n 'marketing' emp.lst
# Count matching lines
grep -c 'director' emp.lst
# Show only filenames with matches
grep -l 'manager' *.lstCode language: PHP (php)
# Multiple patterns from CLI
grep -e 'Agarwal' -e 'aggarwal' -e 'agarwal' emp.lst
# Patterns from a file (one per line)
grep -f pattern.lst emp.lstCode language: PHP (php)
Common pitfalls and best practices
- Quote patterns that contain spaces or shell-special characters.
- Use -i when case is uncertain instead of manually duplicating case variants.
- Remember: -c counts matching lines, not every repeated occurrence on a line.
- Use -f for long pattern lists; it is cleaner and easier to review than many -e flags.
- When redirecting output, use a clear filename (for example, other.list) to avoid shell parsing issues with unquoted spaces.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals