Skip to main content

A few useful grep commands.

# To search for a pattern within a file:
grep "<search_pattern>" <path/to/file>

# To search for an exact string (disables regular expressions):
grep --fixed-strings "<exact_string>" <path/to/file>

# To search for a pattern in all files recursively in a directory, showing line numbers of matches, ignoring binary files:
grep --recursive --line-number --binary-files=<without-match> "<search_pattern>" <path/to/directory>

# To use extended regular expressions (supports '?', '+', '{}', '()' and '|'), in case-insensitive mode:
grep --extended-regexp --ignore-case "<search_pattern>" <path/to/file>

# To print 3 lines of context around, before, or after each match:
grep --<context|before-context|after-context>=<3> "<search_pattern>" <path/to/file>

# To print file name and line number for each match with color output:
grep --with-filename --line-number --color=always "<search_pattern>" <path/to/file>

# To search for lines matching a pattern, printing only the matched text:
grep --only-matching "<search_pattern>" <path/to/file>

# To search stdin for lines that do not match a pattern:
cat <path/to/file> | grep --invert-match "<search_pattern>"

# ---

# Basic search:
grep <pattern> file

# To perform a case insensitive search:
grep -i <pattern> file

# Search recursively (ignoring non-text files) in current directory for an exact string:
grep -RI <search_string> .

# Getting pattern from file (one by line):
grep -f <pattern> file

# Find lines NOT containing <pattern>:
grep -v <pattern> file

# Using regular expressions:
grep "^00" file  # Match lines starting with 00
grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" file  # Find IP add

# Find all files who contain {pattern} in the directory {directory}.
# This will show: "file:line my research"
grep -rnw 'directory' -e "pattern"

# Exclude grep from your grepped output of ps.
# Add [] to the first letter. Ex: sshd -> [s]shd
ps aux | grep '[h]ttpd'


# Search for a given string in a file (case insensitive):
grep -i "the" demo_file

# Print the matched line, along with the 3 lines after it:
grep -A 3 -i "example" demo_text

# Search for a given string in all files recursively:
grep -r "ramesh" *

# Count line when words has been matched:
grep -c 'word' /path/to/file

# Only display filenames of matches:
grep -H -r "redeem reward" /home/tom

# Print all lines containing 'orange ' as a word
grep -w orange data.txt

# Use egrep to search 2 different words
egrep -w 'word1|word2' /path/to/file

# Search for keywork license (case-insensitve) in /dir/path/*, recursively
grep -i -r -s -C 3 'license' /dir/path/* --color

# Count line when words has been matched:
grep -c 'word' /path/to/file

# Precede each line of output with the number of the line matched in file:
grep -n 'root' /etc/passwd

# Print all line that do not contain the word bar (invert match):
grep -v bar /path/to/file

#  List the name of matching files:
grep -l 'main' *.c

# Hide all errors or warning messages generated by the grep command:
grep -w -R 'getMyData()' ~/projects/ 2>/dev/null

# More grep examples here:
# http://www.thegeekstuff.com/2009/03/15-practical-unix-grep-command-examples/