grep
grep is a command that searches for lines matching a regular expression pattern in files or standard input. It is widely used for log analysis and source code searches.
Syntax
grep [options] pattern [file...]
Options
| Option | Description |
|---|---|
| grep pattern file | Displays lines matching the pattern. |
| -i | Performs a case-insensitive search. |
| -r | Searches directories recursively. |
| -v | Displays lines that do not match the pattern (inverted search). |
| -n | Displays line numbers for matching lines. |
| -l | Displays only the names of files with matching lines. |
| -L | Displays only the names of files with no matching lines. |
| -c | Displays only the count of matching lines. |
| -E | Uses extended regular expressions (ERE), equivalent to egrep. |
| -F | Treats the pattern as a fixed string (does not interpret regular expressions). |
| -w | Displays only lines where the pattern matches a whole word. |
| -x | Displays only lines where the pattern matches the entire line. |
| -A N | Also displays N lines after each matching line (After). |
| -B N | Also displays N lines before each matching line (Before). |
| -C N | Also displays N lines before and after each matching line (Context). |
| --include="*.php" | Limits the files searched by extension during recursive search. |
| --exclude-dir=.git | Excludes the specified directory from the search. |
Sample Code
The following file is used in these examples.
app.log
2026-03-05 09:58:10 INFO server started
2026-03-05 10:00:01 ERROR database connection failed
2026-03-05 10:02:15 INFO retrying connection
2026-03-05 10:03:30 WARNING disk usage 85%
2026-03-05 10:05:22 ERROR timeout after 30s
2026-03-05 10:06:00 INFO connection restored
Search for lines containing ERROR in a file (case-sensitive).
grep "ERROR" app.log 2026-03-05 10:00:01 ERROR database connection failed 2026-03-05 10:05:22 ERROR timeout after 30s
Use -i to search without case sensitivity.
grep -i "error" app.log 2026-03-05 10:00:01 ERROR database connection failed 2026-03-05 10:05:22 ERROR timeout after 30s
Use -n to display results with line numbers.
grep -n "ERROR" app.log 2:2026-03-05 10:00:01 ERROR database connection failed 5:2026-03-05 10:05:22 ERROR timeout after 30s
Use -c to display only the count of matching lines.
grep -c "ERROR" app.log 2
Use -v to display lines that do not match the pattern (for example, to exclude comment lines).
grep -v "^#" config.txt host=localhost port=3306 debug=true
Use -E with extended regular expressions to search for multiple keywords.
grep -E "ERROR|WARNING" app.log 2026-03-05 10:00:01 ERROR database connection failed 2026-03-05 10:03:30 WARNING disk usage 85% 2026-03-05 10:05:22 ERROR timeout after 30s
Use -C N to display N lines before and after each matching line.
grep -C 1 "WARNING" app.log 2026-03-05 10:02:15 INFO retrying connection 2026-03-05 10:03:30 WARNING disk usage 85% 2026-03-05 10:05:22 ERROR timeout after 30s
Use -r to search recursively through a directory. Add -l to display only filenames.
grep -rl "TODO" ./src ./src/index.php ./src/utils.php
Use --include to filter files by extension and --exclude-dir to exclude specific directories.
grep -r --include="*.php" --exclude-dir=.git "TODO" . ./src/index.php:// TODO: add validation ./src/utils.php:// TODO: add error handling
Notes
$ grep -r --include="*.php" pattern . is a commonly used pattern for finding where functions and variables are used in PHP projects. Today, faster alternatives such as ripgrep (rg) and ag (The Silver Searcher) are also widely available.
For text substitution, see sed. For field processing, see awk.
If you find any errors or copyright issues, please contact us.