$grep
Search for matching string patterns or regular expressions inside text files.
grep [OPTIONS] PATTERN [FILE...]Try `grep` Live in Interactive Sandbox
Zero-install client simulation with real-time feedback.
Overview & How grep Works
Grep (Global Regular Expression Print) is the standard Unix text search utility. It scans files line by line, matching input against a specified regular expression pattern, and prints matching lines to standard output. It is an indispensable tool for debugging system logs, filtering terminal command outputs via pipes (`|`), and auditing source code for security patterns.
Common Flags & Command Options
| Flag / Option | Description |
|---|---|
| -i | Ignore case distinctions in both patterns and input data. |
| -n | Prefix each matched output line with its 1-based line number. |
| -v | Invert the match to print lines that do NOT match the pattern. |
| -r, -R | Recursively search all files under each directory. |
| -c | Suppress normal output and print only the count of matching lines. |
| -E | Interpret pattern as an Extended Regular Expression (ERE). |
Practical Real-World Examples
grep -i -n "error" /var/log/syslogSearches /var/log/syslog for case-insensitive occurrences of "error" and prints line numbers.
ps aux | grep nginxPipes process snapshot data and isolates lines containing the nginx web server process.
grep -c "Failed password" /var/log/auth.logOutputs the integer count of failed login attempts without listing every individual line.
Common Errors & Troubleshooting
Frequently Asked Questions
What is the difference between grep, egrep, and fgrep?
`grep` uses basic regular expressions, `egrep` (same as `grep -E`) supports extended regex like `+`, `?`, and `|`, and `fgrep` (same as `grep -F`) performs fast fixed-string matching without regex parsing.
Related Linux Commands
Search for files and directories in a directory hierarchy based on name, type, size, or modification date.
Concatenate and print file contents directly to the terminal standard output.
Output the first part (top lines) of text files.
Output the last part (bottom lines) of files or stream real-time logs.
Print newline, word, and byte counts for files.
Print text strings or write data to files using shell redirection.