X
    Categories: Linux

Complete Guide: Linux grep command


Linux grep utility is famous for filtering the output or finding the specific words within a file. This post will touch major options available within Linux grep utility.


Basically Linux grep command is short form of “Global Regular Expression and Print). At the basic level Linux grep command is used for searching or filtering the plain text data using some form of regular expression.

Linux Grep command and Options

Before learning the Linux grep command lets look at the basic syntax of it.

Basic syntax for Linux grep command

cat <file name>|grep <string or regular expression>

<command>|grep <string or regular expression>

grep <string or regular expression> [file name]

Options with Linux grep command

1. Basic search within file

Lets take an example of “/etc/passwd” file for grepping or searching the string within file. To grep or search the word within a file “system” use below command:

[root@rhel1 ~]# cat /etc/passwd|grep system

Sample output:

systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin

2. Counting the occurrence of words.

In the above example we have search the word system in “/etc/passwd” file. And we want to know the count or number of occurrences on system word in the file then use below option:

[root@rhel1 ~]# cat /etc/passwd|grep -c system
2
[root@rhel1 ~]#

The above output states that, the word system has appeared twice in the file “/etc/passwd”

3. Ignore the case sensitive words

Linux grep command is case sensitive meaning that it will only grep the word given in the output. To test this functionality Lets create one flat file named “test.txt” with content as below:

[root@rhel1 tmp]# cat test.txt
UxTechno
uxtechno
UXTECHNO
Uxtechno
[root@rhel1 tmp]#

Now if you try to find or grep the string “uxtechno” it will not list all the word “uxtechno”with different cases as below:

[root@rhel1 tmp]# grep uxtechno test.txt
uxtechno
[root@rhel1 tmp]#

The above result confirms that, only one occurrence has been shown ignoring rest of the “uxtechno” word with different cases. And if you want to ignore this case sensitiveness, you need to use option “-i” with grep as below:

[root@rhel1 tmp]# grep -i uxtechno test.txt
UxTechno
uxtechno
UXTECHNO
Uxtechno

4. Two different strings within Linux grep command

Now if you would like to search two words or strings within Linux grep command. Then you must use egrep command instead of grep. In the below command we are finding two strings “system” and “nobody” in the file /etc/passwd.

[root@rhel1 ~]# egrep  'system|nobody' /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
[root@rhel1 ~]#

5. Recursive search

Lets say you want to search word or string recursively across any directory location , then use option -r. E.g. you want to search word “check_oracle” recursively within /etc directory then use below command:

[root@rhel1 ~]# grep -r "check_oracle" /etc/
/etc/selinux/targeted/contexts/files/file_contexts:/usr/lib/nagios/plugins/check_oracle --      system_u:object_r:nagios_services_plugin_exec_t:s0
Binary file /etc/selinux/targeted/contexts/files/file_contexts.bin matches
/etc/selinux/targeted/modules/active/file_contexts:/usr/lib/nagios/plugins/check_oracle --      system_u:object_r:nagios_services_plugin_exec_t:s0
/etc/selinux/targeted/modules/active/file_contexts.template:/usr/lib/nagios/plugins/check_oracle        --      system_u:object_r:nagios_services_plugin_exec_t:s0
[root@rhel1 ~]

In the above output we can able to see the file name in which we have found the string, and if you want to suppress the file name in the final output then use option “-h” as below:

[root@rhel1 ~]# grep -hr "check_oracle" /etc/
/usr/lib/nagios/plugins/check_oracle    --      system_u:object_r:nagios_services_plugin_exec_t:s0
Binary file /etc/selinux/targeted/contexts/files/file_contexts.bin matches
/usr/lib/nagios/plugins/check_oracle    --      system_u:object_r:nagios_services_plugin_exec_t:s0
/usr/lib/nagios/plugins/check_oracle    --      system_u:object_r:nagios_services_plugin_exec_t:s0
[root@rhel1 ~]#

6. Linux grep for command output.

If you want to search the string or word in any command output then you must use “|” operator followed by grep <string>. Lets say you want to search memory related words in dmesg command output then use below command.

[root@rhel1 ~]# dmesg |grep memory
[    0.000000] Base memory trampoline at [ffff880000098000] 98000 size 24576
[    0.000000] init_memory_mapping: [mem 0x00000000-0x000fffff]
[    0.000000] init_memory_mapping: [mem 0x3fe00000-0x3fffffff]
[    0.000000] init_memory_mapping: [mem 0x3c000000-0x3fdfffff]
[    0.000000] init_memory_mapping: [mem 0x00100000-0x3bffffff]
[    0.000000] kexec: crashkernel=auto resulted in zero bytes of reserved memory.
[    0.000000] Early memory node ranges
[    0.000000] PM: Registered nosave memory: [mem 0x0009e000-0x0009ffff]
[    0.000000] PM: Registered nosave memory: [mem 0x000a0000-0x000dffff]
[    0.000000] PM: Registered nosave memory: [mem 0x000e0000-0x000fffff]
[    0.000000] please try 'cgroup_disable=memory' option if you don't want memory cgroups
[    0.030181] Initializing cgroup subsys memory
[    0.868558] Freeing initrd memory: 34940k freed
[    1.062999] Non-volatile memory driver v1.3
[    1.068621] crash memory driver: version 1.1
[    1.236434] Freeing unused kernel memory: 1620k freed
[    6.902070] [TTM] Zone  kernel: Available graphics memory: 507736 kiB
[root@rhel1 ~]#

7. Invert  Linux grep command

Lets say if you want to display all the words within file which does not contain any specific word, then use option “-v” option. To demonstrate this lets create one file with contents as below:

[root@rhel1 tmp]# cat test.txt
Uxtechno12
Uxtechno454
Uxtechno34343
Uxtechno
LinuxRoutes
Linux
[root@rhel1 tmp]#

Here we don’t want to print the lines having word Linux then use below command.

[root@rhel1 tmp]# grep -v Linux test.txt
Uxtechno12
Uxtechno454
Uxtechno34343
Uxtechno

8. Exact match word

As per example given in the step 7, if we search for Uxtehno then it will print all the occurence of Uxtehno like “Uxtechno12” , “Uxtechno454” , “Uxtechno34343” and also “Uxtechno”  as below:

[root@rhel1 tmp]# grep  Uxtechno test.txt
Uxtechno12
Uxtechno454
Uxtechno34343
Uxtechno
[root@rhel1 tmp]#

what if we want to search exact word “Uxtechno” instead of all above listed output, then use option “-w” as below:

[root@rhel1 tmp]# grep -w Uxtechno test.txt
Uxtechno
[root@rhel1 tmp]#

View Comments (0)

Related Post