Introduction
Hello! In this article, we’ll learn about the grep command.
grep is a command that searches for specific patterns in text files or command output. It’s super useful when you’re like “Where was that string in this file again?”, and it’s definitely one of the must-know commands for using Linux.
What is the grep Command?
grep stands for “Global Regular Expression Print” and is a command that searches files or text for lines matching a specified pattern (strings or regular expressions) and displays them.
It has tons of use cases - finding error messages in log files, locating specific settings in configuration files, searching for function names in code, and so much more.
Basic Syntax
|
|
Main Options
| Option | Description |
|---|---|
-i |
Ignore case (case-insensitive search) |
-v |
Display lines that don’t match (inverted search) |
-n |
Display line numbers |
-c |
Count matching lines |
-l |
Display only filenames with matches |
-r |
Recursively search directories |
-E |
Use extended regular expressions (same as egrep) |
-w |
Match whole words only |
-A num |
Display num lines after match |
-B num |
Display num lines before match |
-C num |
Display num lines before and after match |
Usage Examples
Example 1: Basic Search
|
|
Output:
|
|
This displays all lines containing “error” from the log.txt file. This is the most basic usage.
Example 2: Case-Insensitive Search
|
|
Output:
|
|
Using the -i option matches “Error”, “ERROR”, “error” - all of them. Super convenient.
Example 3: Display with Line Numbers
|
|
Output:
|
|
The -n option also displays line numbers. Useful when you want to know where things are.
Example 4: Display Non-Matching Lines
|
|
Output:
|
|
The -v option does inverted search. In this example, it displays lines that don’t start with # (comment lines).
Example 5: Count Matching Lines
|
|
Output:
|
|
Counts how many lines contain “TODO” in each file. Useful for checking the number of tasks.
Example 6: Display Filenames Only
|
|
Output:
|
|
Displays only filenames with matches. Convenient when you want to know which files contain something.
Example 7: Recursive Search
|
|
Output:
|
|
The -r option searches everything under the ./src directory. Super convenient for searching the entire project.
Example 8: Search Using Regular Expressions
|
|
Output:
|
|
The -E option enables extended regular expressions. This example searches for phone number patterns.
Example 9: Match Whole Words
|
|
The -w option matches whole words only. “cat” matches, but “category” doesn’t.
Example 10: Display Surrounding Lines
|
|
Output:
|
|
-C 2 displays 2 lines before and after the match. Useful when you want to see the context around errors.
Example 11: Combine with Pipes
|
|
Output:
|
|
Filter the output of other commands with grep. I use this a lot.
Basic Regular Expression Patterns
| Pattern | Meaning |
|---|---|
. |
Any single character |
^ |
Beginning of line |
$ |
End of line |
* |
Zero or more of preceding character |
+ |
One or more of preceding character (requires -E) |
? |
Zero or one of preceding character (requires -E) |
[abc] |
Any of a, b, c |
[^abc] |
Any except a, b, c |
[0-9] |
Digit |
| |
OR (requires -E) |
Tips & Notes
-
Use quotes: Wrap patterns containing spaces or special characters in quotes
1grep "error message" log.txt -
Characters that need escaping: Escape special characters like
.or*with\when searching for them literally1grep "192\.168\.1\.1" config.txt -
Colored display:
--color=autohighlights search results (default in many environments)1grep --color=auto "error" log.txt -
Multiple pattern search: Use
-eoption to specify multiple patterns1grep -e "error" -e "warning" log.txt -
Read patterns from file: Use
-foption to read patterns from a file1grep -f patterns.txt log.txt
Practical Usage
Extract Errors from Log Files
|
|
Picks up all error-related messages from system logs. I use this a lot for troubleshooting.
Exclude Comments and Blank Lines from Config Files
|
|
Convenient when you only want to see actual settings in a config file. Excludes comment lines and blank lines.
Check Processes
|
|
A technique to exclude the grep process itself. The regex [n]ginx matches “nginx” but not this command’s own “[n]ginx”.
Search for Specific Code Across Multiple Files
|
|
Can be used to find function definitions in JavaScript files within a project.
List All TODO Comments
|
|
Lists all TODO comments in the codebase. Useful before refactoring.
Search Excluding Specific Extensions
|
|
Searches while excluding minified files and node_modules. Reduces unnecessary results.
Summary
In this article, we learned about the grep command.
Key Points:
grepis a basic command for searching patterns in text- Use
-ito ignore case,-nto display line numbers - Use
-rto recursively search directories - Supports regular expressions for complex pattern searches
- Combine with pipes to filter output from other commands
grep is truly an essential command for using Linux. You’ll use it constantly - for log investigation, code searching, and so many other scenarios. Try out the various options and get comfortable using it.
Keep learning Linux commands!