The grep
(Global Regular Expression Print) command is one of the most powerful tools in Linux. It searches for specific patterns in files or outputs and shows lines that match, making it extremely helpful for reading text or log files.
Basic Syntax
grep [options] 'pattern' [file...]
Basic Examples
- Simple Search:
grep 'pattern' filename
This searches for pattern
in filename
and shows the matching lines.
- Case-Insensitive Search:
grep -i 'pattern' filename
The -i
option ignores uppercase and lowercase differences.
- Search for Exact Words:
grep -w 'pattern' filename
The -w
option searches for whole words only.
Searching Multiple Files
- Search Across Multiple Files:
grep 'pattern' file1 file2 file3
This shows matches from each file and labels them by filename.
- Recursive Search in Directories:
grep -r 'pattern' /path/to/directory
The -r
option searches all files in a directory and its subdirectories.
Advanced Pattern Matching
- Match Single Characters:
grep 'p.ttern' filename
The .
symbol matches any single character, so p.ttern
will match words like pattern
, pxttern
, etc.
- Match Multiple Patterns:
grep -E 'pattern1|pattern2' filename
The -E
option allows more complex patterns. This example matches either pattern1
or pattern2
.
Line Numbers and Context
- Show Line Numbers:
grep -n 'pattern' filename
The -n
option shows the line number for each match.
- Show Lines Before/After Matches:
grep -A 3 'pattern' filename # 3 lines after match
grep -B 3 'pattern' filename # 3 lines before match
grep -C 3 'pattern' filename # 3 lines before and after match
The options -A
, -B
, and -C
allow you to see lines around your match, which is useful for context.
Counting and Excluding Matches
- Count Matching Lines:
grep -c 'pattern' filename
The -c
option shows the count of matching lines.
- Exclude Matches:
grep -v 'pattern' filename
The -v
option shows lines that do not match the pattern.
Using Multiple Patterns
You can create a file to store multiple patterns and search for all of them:
grep -f patternfile filename
The patternfile
contains each pattern on a new line, and grep
will search for them all in filename
.
Practical Use Cases
- Search System Logs for Errors:
grep -i 'error' /var/log/syslog
Finds all lines containing “error” (ignores case) in the system log.
- Find Running Processes with a Specific Name:
ps aux | grep 'process_name'
- Remove Blank Lines from a File*:
grep -v '^$' filename
This pattern (^$
) matches empty lines, and the -v
option removes them from the output.