grep
Search for text patterns in files and command output using grep with regular expressions on Linux and macOS.
- What grep Does and When to Use It
- Core Concepts of grep
- Basic vs Extended Regular Expressions in grep
- Recursive Search with grep
- Common Tasks with grep
- How to Search for a String in a File with grep
- How to Search Case-Insensitively with grep
- How to Count Matching Lines with grep
- grep Troubleshooting
- Related Tools and Guides
grep is a command-line text search utility that finds lines matching a pattern in files or piped input using literal strings or regular expressions on Linux and macOS.
What grep Does and When to Use It
The grep command (Global Regular Expression Print) searches through text input and prints every line that matches a specified pattern. System administrators use grep to search log files for error messages, filter command output for specific entries, and find configuration values across multiple files.
grep is the universal filter in the Unix pipeline. Commands like
cat /var/log/syslog | grep "error",
ps aux | grep nginx, and
find /etc -name "*.conf" | xargs grep "listen" combine grep with other tools to answer complex questions about system state.
grep is not a file editor — it finds text but does not modify it. Use
sed for find-and-replace operations. grep is not optimized for structured data like JSON or CSV — use
jq for JSON and
awk for columnar data.
Core Concepts of grep
Basic vs Extended Regular Expressions in grep
grep supports two regex syntaxes. Basic Regular Expressions (BRE) require escaping special characters:
grep 'error\|warning'. Extended Regular Expressions (ERE), activated with
-E (or the
egrep alias), use unescaped operators:
grep -E 'error|warning'. For literal string matching without regex, use
-F (or the
fgrep alias):
grep -F 'error|warning' searches for the literal text
error|warning.
Recursive Search with grep
The
-r flag searches all files in a directory tree recursively. Combine with
--include to limit by file extension:
grep -r --include="*.conf" "listen" /etc/nginx/. The
-l flag prints only filenames containing matches, without the matching lines.
Common Tasks with grep
How to Search for a String in a File with grep
grep "error" /var/log/syslogHow to Search Case-Insensitively with grep
grep -i "warning" /var/log/apache2/error.logHow to Count Matching Lines with grep
grep -c "404" /var/log/nginx/access.logFor detailed instructions, see How to search for a string in multiple files with grep.
grep Troubleshooting
| Error | Cause | Fix |
|---|---|---|
No such file or directory | Filename globbing expanded to zero results, or quoting issue | → Full article |
Invalid regular expression | Unescaped special characters, or BRE/ERE mismatch | → Full article |
Binary file matches | File contains non-text data; grep suppresses binary output | → Full article |
Related Tools and Guides
Logrotatecompresses and rotates log files that grep searches. Nginxand Apacheproduce access and error logs that grep filters. See grep vs egrep vs fgrep vs ripgrepfor choosing the right search tool.