Linux operating system »The grep command in Linux. The complete guide

The Linux grep utility is for filtering the output or searching for a specific word in a file. This post will cover the basic options available for the grep utility in Linux.

Grep short form (Global Regular Expression and Print). At a basic level grep command in Linux are used to search or filter simple text data using some form of regular expression.

Linux grep command and options

Before examining the grep command, let’s take a look at the basic syntax for it.

Basic syntax for 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 the grep command

1. Simple file search

Let’s look at an example in the / etc / passwd file to find a string in a file. To find the word “system” with grep, use the command:

[[email protected] ~]# cat /etc/passwd|grep system

Output example:

systemd-bus-proxy:x:899:897:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:898:896:systemd Network Management:/:/sbin/nologin

2. Counting the occurrence of words.

In the above example, we have in the system a search for words in a file

e “/ etc / passwd”. If we want to know the number or number of occurrences of a word in the file, then use the option below:

[[email protected] ~]# cat /etc/passwd|grep -c system
2
[[email protected] ~]#

Above, it is stated that the word appeared twice in the file “/ etc / passwd”.

3. Ignore case sensitive words

Grep is case sensitive, which means that it will only search for the given word in the file. To test this function, create one file named “test.txt” and with the content as shown below:

[[email protected] tmp]# cat test.txt
AndreyEx
andreyex
ANDREYEX
Andreyex
[[email protected] tmp]#

Now, if you try to find the string “grayex”, then the command will not list all the words “grayex” with different variations, as shown below:

[[email protected] tmp]# grep andreyex test.txt
andreyex
[[email protected] tmp]#

This result confirms that only one variant will be shown, ignoring the rest of the word “andreyex” with different variants. And if you want to ignore this case, you need to use the “-i” option with grep as shown below:

[[email protected] tmp]# grep -i andreyex test.txt
AndreyEx
andreyex
ANDREYEX
Andreyex

4. Two different lines inside the file with the grep command

Now, if you want to search for two words or lines using the grep command, then you must specify extended. In the next command, we find two lines “system” and “nobody” in the / etc / passwd file.

[[email protected] ~]# grep  'system|nobody' /etc/passwd
nobody:x:89:89:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:899:897:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:898:896:systemd Network Management:/:/sbin/nologin
[[email protected] ~]#

5. Recursive search

Let’s say you want to find a word or string recursively anywhere in a directory, then use the -r option. For example, if you want to find the word “check_oracle” recursively in the / etc directory, then use the following command:

[[email protected] ~]# 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
[[email protected] ~]

In the output above, we can be able to see the name of the file in which we found the line, and if you want to remove the file name from the final result, then use the “-h” option, as shown below:

[[email protected] ~]# 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
[[email protected] ~]#

6. Output of the grep command.

If you want to search for a line or word in any output of a command, then you must use the “|” operator followed by in grep. Let’s say you want to search in memory for the associated words of the dmesg command output, then use the following command.

	
[[email protected] ~]# dmesg |grep memory
[    0.000000] Base memory trampoline at [ffff880000098000] 98000 size 19456
[    0.000000] init_memory_mapping: [mem 0x00000000-0x000fffff]
[    0.000000] init_memory_mapping: [mem 0x3fe00000-0x4fffffff]
[    0.000000] init_memory_mapping: [mem 0x3c000000-0x4fdfffff]
[    0.000000] init_memory_mapping: [mem 0x00100000-0x4bffffff]
[    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 0x0003e000-0x0003ffff]
[    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.862358] Freeing initrd memory: 23532k freed
[    1.064599] Non-volatile memory driver v1.3
[    1.069351] crash memory driver: version 1.1
[    1.186673] Freeing unused kernel memory: 1430k freed
[    5.567780] [TTM] Zone  kernel: Available graphics memory: 480345 kiB
[[email protected] ~]#

7. Inverting with grep command in Linux

Suppose if you want to display all words in a file that does not contain any particular word, then use the “-v” option. This allows you to create one file with content as shown below:

[[email protected] tmp]# cat test.txt
Andreyex12
Andreyex454
Andreyex34343
Andreyex
LinuxRoutes
Linux
[[email protected] tmp]#

If we don’t want to print lines containing the word Linux, then use the following command.

[[email protected] tmp]# grep -v Linux test.txt
Andreyex12
Andreyex454
Andreyex34343
Andreyex

8. Exact word match

According to the example in point 7, if we search for Andreyex, then it will print all occurrences of Andreyex as “Andreyex12”, “Andreyex454”, “Andreyex34343” and also “Andreyex” as shown below:

[[email protected] tmp]# grep  Andreyex test.txt
Andreyex12
Andreyex454
Andreyex34343
Andreyex
[[email protected] tmp]#

then if we want to find the exact word “Andreyex” instead to list all the output above then use the “-w” option as shown below:

[[email protected] tmp]# grep -w Andreyex test.txt
Andreyex
[[email protected] tmp]#

Sidebar