Online Linux Terminal Logo
OnlineLinuxTerminal
Text ProcessingCLI Reference

$grep

Search for matching string patterns or regular expressions inside text files.

Syntax DefinitionPOSIX standard
grep [OPTIONS] PATTERN [FILE...]
Linux Community2.4k Online

Chat with real Linux users, learn new commands & get free cheat sheet PDFs.

Try `grep` Live in Interactive Sandbox

Zero-install client simulation with real-time feedback.

Open in Fullscreen Workspace↗
bash — terminal

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 / OptionDescription
-iIgnore case distinctions in both patterns and input data.
-nPrefix each matched output line with its 1-based line number.
-vInvert the match to print lines that do NOT match the pattern.
-r, -RRecursively search all files under each directory.
-cSuppress normal output and print only the count of matching lines.
-EInterpret pattern as an Extended Regular Expression (ERE).

Practical Real-World Examples

Example #1Search for errors in system log files
grep -i -n "error" /var/log/syslog

Searches /var/log/syslog for case-insensitive occurrences of "error" and prints line numbers.

Sample Output:
14: Aug 28 10:14:02 kernel: [ERROR] Network timeout on eth0
Example #2Filter process list through a pipe
ps aux | grep nginx

Pipes process snapshot data and isolates lines containing the nginx web server process.

Example #3Count total failed authentication attempts
grep -c "Failed password" /var/log/auth.log

Outputs the integer count of failed login attempts without listing every individual line.

Sample Output:
42

Common Errors & Troubleshooting

grep: /path/to/dir: Is a directory
Cause: Targeted a directory without specifying the recursive flag.
Solution: Add the `-r` or `-R` flag when searching folders (e.g., `grep -r "keyword" /etc/`).

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.