grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines
Grep Commands
Grep and its Options
-v = non-matching lines
-c = count of matching lines
-i = Ignore case
-r = Read all files under each directory
-l = list the file which contain the searching keyword
^ = Search the Starting word of a file
.$ = Search the end word of a file
^$ = Search the empty lines in a file
Find all lines matching a specific keyword on a file.
grep sysadmin /etc/passwd
Display with numbers
grep -nv nologin /etc/passwd
To avoid the keyword and search for other
grep -v sysadmin /etc/passwd
To count how many lines match for the keywords we are searching
grep -c sysadmin /etc/passwd
To search text by ignoring the case
grep -i sysadmin /etc/passwd
Search all files in /home
and its subdirectories for text matching a specific pattern and print the matching lines
grep -ri sysadmin /home/
Search all files in /home
and its subdirectories for text matching a specific pattern and list the files which contain it
grep -ril sysadmin /home/
To search for a particular IP mentioned among many IPs. The -F
ensures that the .
match literal .
characters, without it they would match any character.
grep -F "192.168.1.10" /var/log/syslog
To search for a line that starts with Feb
grep ^Feb /var/log/syslog
To search for a line that ends with queue
grep queue$ /var/log/mail.log
To count the empty lines in a file (lines containing whitespace will not be counted)
grep -c ^$ /var/log/mail.log
Search for a string inside all files of a directory and its subdirectories
grep -r "root" .
man grep
, everything you need to know is there. – terdon Jun 17 '14 at 13:26