Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. grep

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

OptionDescription
grep pattern fileDisplays lines matching the pattern.
-iPerforms a case-insensitive search.
-rSearches directories recursively.
-vDisplays lines that do not match the pattern (inverted search).
-nDisplays line numbers for matching lines.
-lDisplays only the names of files with matching lines.
-LDisplays only the names of files with no matching lines.
-cDisplays only the count of matching lines.
-EUses extended regular expressions (ERE), equivalent to egrep.
-FTreats the pattern as a fixed string (does not interpret regular expressions).
-wDisplays only lines where the pattern matches a whole word.
-xDisplays only lines where the pattern matches the entire line.
-A NAlso displays N lines after each matching line (After).
-B NAlso displays N lines before each matching line (Before).
-C NAlso displays N lines before and after each matching line (Context).
--include="*.php"Limits the files searched by extension during recursive search.
--exclude-dir=.gitExcludes 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

📁 ~/project/src/ 📄 index.php (contains "TODO") 📄 utils.php (contains "TODO") 📄 config.txt (contains comment lines)

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 .