Linux grep command Examples

Linux grep is another very usefull tool in linux system for searching text or the given pattern and then display the matching lines. How Do I user linux grep command to search text from a file or multiple files? How to use linux grep command under linux operating system? this article will guide you how to use linux grep command.

NAME

grep, egrep, fgrep – print lines matching a pattern

​SYNOPSIS

       grep [OPTIONS] PATTERN [FILE...]

       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION

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.

Examples:

#1 Using linux grep command to search a file 

type:

grep root /etc/passwd

outputs:

[root@devops ~]# grep root /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

#2 Ignore case distinctions to grep a line from a file

You need to “-i” options to grep command to search a file.

type:

grep -i "root" /etc/passwd

outputs:

[root@devops ~]# grep -i  root /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

#3 grep all files under eachdirectory recursively 

type:

grep -r "/root" /etc

outputs:

[root@devops ~]# grep -r "/root" /etc

Binary file /etc/pki/ca-trust/extracted/java/cacerts matches

/etc/samba/smb.conf:# To run scripts (preexec/root prexec/print command/...), copy them to the

/etc/passwd:root:x:0:0:root:/root:/bin/bash

/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin

/etc/sysconfig/raid-check:# /dev/md/root.  The names used in this file must match the names seen in

/etc/default/grub:GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/swap vconsole.font=latarcyrheb-sun16 

rd.lvm.lv=centos/root crashkernel=auto  vconsole.keymap=us rhgb quiet"

/etc/passwd-:root:x:0:0:root:/root:/bin/bash

/etc/passwd-:operator:x:11:0:operator:/root:/sbin/nologin

/etc/ppp/eaptls-client:#clientserver/root/cert/client.crt-

/root/cert/ca.crt/root/cert/client.key

/etc/ppp/eaptls-server:#clientserver-/root/cert/server.crt

/root/cert/ca.crt/root/cert/server.key 192.168.1.0/24

...

#4 suppress the prefixing of file names on output while searching a file 

You can use “-h” option to linux grep command for suppressing the outputs of searching,type:

grep -h -r "root" /etc

outputs:

[root@devops ~]# grep -h -r "/root" /etc

Binary file /etc/pki/ca-trust/extracted/java/cacerts matches

# To run scripts (preexec/root prexec/print command/...), copy them to the

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

# /dev/md/root.  The names used in this file must match the names seen in

GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/swap vconsole.font=latarcyrheb-sun16 rd.lvm.lv=centos/root 

crashkernel=auto  vconsole.keymap=us rhgb quiet"

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

#client server /root/cert/client.crt - /root/cert/ca.crt /root/cert/client.key

#clientserver-/root/cert/server.crt/root/cert/ca.crt/root/cert/server.key

192.168.1.0/24

ignoredirs=/root

/root(/.*)? system_u:object_r:admin_home_t:s0

/root/.ppprc -- system_u:object_r:pppd_etc_t:s0

/root/.manpath -- system_u:object_r:mandb_home_t:s0

/root/bin(/.*)? system_u:object_r:bin_t:s0

#5 Using linux grep command to search only those lines that containing matchs that form whole words

If you want to search a words only, you need to use “-w” option to grep command, type:

grep -w "root" /etc/passwd

outputs:

[root@devops ~]# grep -w  "root" /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

#6 using grep command to print the number of searching output context.

If you want to know the number when searching words has been matched, you need to use “-c” option to linux grep command, type:

grep -c "root" /etc/passwd

outputs:

[root@devops ~]# grep -c "root" /etc/passwd
2

#7 using grep command to print each line number of outputs

You need to pass the “-n” options to linux grep command to print each line number of outputs that

matching the words,type:

grep -n "root" /etc/passwd

outputs:

[root@devops ~]# grep -n "root" /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

10:operator:x:11:0:operator:/root:/sbin/nologin

#8 Using grep command to invert the sense of matching 

You can use the “-v” option to grep command to invert match to select non-matching line,type:

grep -v "root" /etc/passwd

outputs:

[root@devops ~]# grep -v "root" /etc/passwd

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

...

#9 using grep command to list just names of matching files 

If you just want to list the file name that the content was matched, you can use “-l” options to grep command, type:

grep -l "root" /etc/*.ow

outputs:

[root@devops ~]# grep -l "root" /etc/*ow

/etc/gshadow

/etc/shadow

 

You might also like:

Sidebar



back to top