GREP trips and tips
-
Search for a string in a file
$ grep root /etc/passwd root:x:0:0:root:/root:/bin/bash -
case insensitive
$ grep -i ROOT /etc/passwd root:x:0:0:root:/root:/bin/bash -
wrap search string around quotes
$ grep -i "gnats bug" /etc/passwd gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin -
count
$ grep -c root /etc/passwd 1 -
line number
$ grep -n root /etc/passwd 1:root:x:0:0:root:/root:/bin/bash -
recursive search
$ grep -r "mydomain.com" /etc/nginx/ /etc/nginx/sites-available/mydomain.com.vhost: if ($http_host != "www.mydomain.com") { -
multiple matches
$ egrep "root|news" /etc/passwd root:x:0:0:root:/root:/bin/bash news:x:9:9:news:/var/spool/news:/usr/sbin/nologin -
inverted matches (filter out)
$ grep -v root /etc/passwd prints everything except for lines with root -
quietly test
$ grep -q root /etc/passwd $ echo $? 0