-i
: Ignore case (case-insensitive search)-w
: Match whole word-n
: Show line numbers-c
: Count the number of matching lines-v
: Invert match, show lines that do not match-r
: Search files recursively in subdirectories-l
: Show only the filenames of matching files-h
: Do not show filenames in output-e pattern
: Use pattern as the search pattern-f file
: Read the search pattern from a file-E
: Interpret the pattern as an extended regular expression-P
: Interpret the pattern as a Perl-compatible regular expression-m num
: Stop after finding num matches-A num
: Show num lines of trailing context after the match-B num
: Show num lines of leading context before the match-C num
: Show num lines of context before and after the match
.
: Match any single character^
: Match the beginning of a line$
: Match the end of a line[]
: Match any character inside the brackets[^]
: Match any character NOT inside the brackets()
: Group characters together|
: Match either/or (e.g.cat|dog
)*
: Match zero or more of the preceding character+
: Match one or more of the preceding character?
: Match zero or one of the preceding character{}
: Match a range of occurrences (e.g.a{1,3}
matches "a", "aa", or "aaa")
- Search for a specific word or pattern:
grep 'pattern' file.txt
- Search across multiple files:
grep 'pattern' file1.txt file2.txt
- Search recursively in a directory:
grep -r 'pattern' /path/to/directory/
- Ignore case when matching patterns:
grep -i 'pattern' file.txt
- Show lines that do not match the pattern:
grep -v 'pattern' file.txt
- Match only whole words in a file:
grep -w 'word' file.txt
- Print 3 lines before each match:
grep -B 3 'pattern' file.txt
- Print 3 lines after each match:
grep -A 3 'pattern' file.txt
- Print 3 lines before and after each match:
grep -C 3 'pattern' file.txt
- Display only the count of matching lines:
grep -c 'pattern' file.txt
- Display line numbers for matches:
grep -n 'pattern' file.txt
- Print filenames with matches:
grep -H 'pattern' file.txt
- Match any of several patterns:
grep -e 'pattern1' -e 'pattern2' file.txt
- Use a file with multiple patterns:
grep -f patterns.txt file.txt
- Use extended regex patterns:
grep -E 'pattern1|pattern2' file.txt
- Interpret patterns as Perl-compatible regex:
grep -P 'pattern' file.txt
- Enable colored output for matches:
grep --color=auto 'pattern' file.txt
- Print only the matching portion of lines:
grep -o 'pattern' file.txt
- Search using fixed strings (faster for literals):
grep -F 'exact string' file.txt
- Print only the filenames containing matches:
grep -l 'pattern' *.txt
- Print filenames that do not contain matches:
grep -L 'pattern' *.txt
- Search for lines starting with "Error":
grep '^Error' log.txt
- Find lines ending with ".com":
grep '\.com$' emails.txt
- Match lines containing digits:
grep '[0-9]' file.txt
- Exclude empty lines:
grep -v '^$' file.txt