7 useful grep commands
Grep is a command-line utility that allows you to search for patterns in text files. Here are some common grep commands and their explanations:
grep 'error' logfile.txt
: This command searches the filelogfile.txt
for lines that contain the word "error".grep -r 'error' /var/log
: This command searches the/var/log
directory and all subdirectories for files that contain the word "error". The-r
option stands for "recursive."grep -v 'warning' logfile.txt
: This command searches the filelogfile.txt
for lines that do not contain the word "warning". The-v
option stands for "invert match."grep -i 'ERROR' logfile.txt
: This command searches the filelogfile.txt
for lines that contain the word "ERROR", ignoring the case of the characters. The-i
option stands for "ignore case."grep -c 'error' logfile.txt
: This command searches the filelogfile.txt
for lines that contain the word "error" and prints a count of the number of matching lines. The-c
option stands for "count."grep -n 'error' logfile.txt
: This command searches the filelogfile.txt
for lines that contain the word "error" and prints the line numbers of the matching lines. The-n
option stands for "line number."grep -l 'error' *.log
: This command searches all files with the.log
extension in the current directory for lines that contain the word "error" and prints only the names of the files that contain a match, rather than the matching lines themselves. The-l
option stands for "files with matches."