How to Use the grep Command in Linux
Introduction
The grep
command in Linux is a sorting utility used to search for text patterns in files. It's also known as global regular expression print (grep
) and actively matches patterns in output file types such as configurations, text, or application code.
This article explains how to use the grep
command in Linux to search and manipulate text in files.
grep
Command Syntax
Below is the grep
command syntax:
$ grep [options] pattern [file...]
In the above command, pattern
is the target text or regular expression to look up while [file...]
specifies one or more files to search.
grep
Command Options
Option | Description |
---|---|
-i |
Ignore case distinctions in the pattern. |
-v |
Invert the match, displaying lines that do not match a pattern. |
-c |
Count the number of matching lines. |
-l |
List filenames with matching lines. |
-n |
Display line numbers with matching lines. |
-r |
Recursively search directories. |
-w |
Match whole words only. |
-x |
Match whole lines only. |
-A |
Display N lines of context after the match. |
-B |
Display N lines of context before the match. |
-C |
Display N lines of context around the match. |
Practical Examples of the grep
Command
Perform a basic search.
console$ grep "Vultr" file.txt
The above command searches for the string
Vultr
infile.txt
and displays all matching results in the file.Output:
Ignore case sensitivity.
console$ grep -i "Vultr" file.txt
The above command looks up for the text
Vultr
and displays all results that match any case. For example, text such asvultr
,vULTR
, andVULTR
match the search.Output:
Invert match and display all lines that do not include a specific string.
console$ grep -v "Vultr" file.txt
The above command displays all lines that don't include the string
Vultr
.Output:
Count lines with matching pattern.
console$ grep -c "Vultr" file.txt
The above command counts the number of lines that include the text
Vultr
.Output:
List director files that match a specific filename order.
console$ grep -l "Hello" *.txt
The above command lists all files in the working directory that include the string
Hello
.Output:
Show line numbers.
console$ grep -n "Vultr" file.txt
The above command displays all lines that match the string
Vultr
and the respective line numbers.Output:
Perform a recursive search.
console$ grep -r "Hello" /path/to/directory
The above command searches for the string
Hello
in all files within a specific directory and all sub-directories.Output:
Match whole words.
console$ grep -w "Vultr" file.txt
The above command searches for the string
Vultr
as a whole word in the filefile.txt
.Output:
Using Regular Expressions with grep
The grep
command supports regular expressions (Regex) that describe a sets of strings to search with advanced pattern matching methods.
Search for a string at the beginning of a line.
console$ grep "^Vultr" file.txt
The above command matches and displays all lines that start with the string
Vultr
.Output:
Search for a string at the end of a line.
console$ grep "rates.$" file.txt
The above command matches and displays lines that end with the string
rates.
.Output:
Search for lines that containing a range of number from between
0
and9
.console$ grep "[0-9]" file.txt
The above command matches and displays the lines that contain any number in the specified range.
Output:
Advanced Usage Scenarios
Search for multiple patterns.
console$ grep -e "Agrawal" -e "Aggrawal" file.txt
The above command searches for all lines that contain either
Agrawal
orAggrawal
.Output:
Combine
grep
with other commands to filter results.console$ ps aux | grep "process_name"
The above command searches for all processes and uses
grep
to filter all running processes with the stringprocess_name
.Output:
Use
grep
withfind
.console$ find /path/to/directory -type f -exec grep "example" {} +
The above command searches for the string
example
in thefind
command output.Output:
Display lines with context.
console$ grep -C 3 "Vultr" file.txt
The above command displays three lines of context on each matching line.
Output:
Search compressed files.
console$ zgrep "pattern" file.gz
The above command searches for the string
pattern
in the compressedfile.gz
file.Output:
Exclude specific files from search.
console$ grep --exclude="*.log" -r "pattern" /path/to/directory
The above command recursively searches for
pattern
but excludes files with the.log
extension.Output:
Conclusion
You have used the grep
command to efficiently search and manipulate textual data in files and directories. For more command options, the man grep
command to view the grep
manual page.