The grep command on Linux systems is one of the most common commands you’ll come across. If we had to sum up this command, we’d say it’s used to find a specified string or text inside inside of a file. But even with a simple explanation like that, the amount of things it can be used for is quite staggering.
The grep
command also has a few close cousins, in case you find that it’s not up to the job. That’s where commands like egrep
, fgrep
, and rgrep
come in handy. These commands all work similarly to grep
, but extend its functionality and sometimes simplify its syntax. Yes, it sounds confusing at first. But don’t worry, we’ll help you master the alphabet of grep commands in this guide.
In this tutorial, we’ll go over various command examples for grep
, egrep
, fgrep
, and rgrep
on Linux. Read on to see how these commands work, and feel free to use them on your own system as we go along so you can become acquainted with them.
In this tutorial you will learn:
- Command examples for grep, egrep, fgrep, rgrep
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | grep, egrep, fgrep, rgrep |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
grep
For our examples, we’ve created a simple text document named distros.txt
that contains a bunch of names of Linux distros. See below how we use grep
and its related commands to search this file for certain text and patterns.
- As mentioned before,
grep
can be used to search for a string within a file. Lets search for the word “Ubuntu”:$ grep Ubuntu distros.txt Ubuntu
- As everything else in Linux,
grep
is also case sensitive. To ignore case we need to usegrep
with combination of-i
option:$ grep -i ubuntu distros.txt Ubuntu Kubuntu Xubuntu
- The
-n
option will show which line number each match was found on.$ grep -i -n ubuntu distros.txt 3:Ubuntu 8:Kubuntu 9:Xubuntu
- We can also use the
-v
(invert) option to show lines that don’t match our search pattern.$ grep -iv ubuntu distros.txt Arch Linux AlmaLinux Fedora Red Hat Enterprise Linux CentOS Linux Mint Debian Manjaro openSUSE
As you can see, all the distros are listed except the ones that contained “Ubuntu” (case insensitive).
- With the
-c
option, grep can count the number of string occurrences within files. So here the grep will print the number of how many times Ubuntu does NOT appear within the file:$ grep -ivc ubuntu distros.txt 9
- The
-x
option will print exact occurrences only.$ grep -ix ubuntu distros.txt Ubuntu
- System administrators will definitely appreciate this example when searching log files.
-B3
( display 3 lines before match ) and-A3
( display 3 lines after match ) will give your output more context.$ grep -B3 -A3 command /var/log/dmesg [ 0.201120] kernel: pcpu-alloc: [0] 0 [ 0.201186] kernel: Built 1 zonelists, mobility grouping on. Total pages: 515961 [ 0.201188] kernel: Policy zone: DMA32 [ 0.201191] kernel: Kernel command line: BOOT_IMAGE=/boot/vmlinuz-5.8.0-59-generic root=UUID=a80ad9d4-90ff-4903-b34d-ca70d82762ed ro quiet splash [ 0.201563] kernel: Dentry cache hash table entries: 262144 (order: 9, 2097152 bytes, linear) [ 0.201648] kernel: Inode-cache hash table entries: 131072 (order: 8, 1048576 bytes, linear) [ 0.201798] kernel: mem auto-init: stack:off, heap alloc:on, heap free:off
grep and regex
grep and regular expressions is a topic that can cover a whole book, but it would be shame to not show at least a couple examples for grep and regular expressions.
- To make
grep
return only lines which contains digits, we would use the command:$ grep [0-9] file.txt
- To count all empty lines within a file using
grep
we use this command:$ grep -ch ^$ file.txt
- Let’s see what line starts with “L” and ends with a number.
^
is used to match the beginning of a line, and$
is used to match the end of a line:$ grep ^L.*[0-9]$ file.txt
- To make
grep
match only lines where “b” is a third character in the word, we can use the following command:$ grep ..b file.txt
egrep
egrep
is the extended version of grep
. In other words, egrep
is equal to grep -E
. egrep supports more regular expression patterns.
- Let’s search for lines that contain exactly two consecutive “p” characters:
$ egrep p{2} file.txt OR $ grep pp file.txt OR $ grep -E p{2} file.txt
- Let’s get an output of
egrep
command of all lines that end with “S” or “A”:
$ egrep "S$|A$" file.txt
fgrep
fgrep
is a faster version of grep
which does not support regular expressions and therefore is considered to be faster. fgrep
is equal to grep -F
. This is handy to use in scripts or against large files where you don’t need the extra robustness of normal grep
, as the results should be returned faster, and with a lesser impact on system resources.
- You can only use simple pattern searching with this tool, such as the following:
$ fgrep Fedora distros.txt Fedora
- Expressions will NOT work and will simply return blank output.
$ fgrep -i linux$ distros.txt $ grep -i linux$ distros.txt Arch Linux AlmaLinux Red Hat Enterprise Linux
rgrep
rgrep
is a recursive version of grep
. Recursive in this case means that rgrep can recursively descend through directories as it greps for the specified pattern. rgrep
is similar to grep -r
.
- Search all files, recursively for a string “linux”.
$ rgrep -i linux * dir1/RHEL-based.txt:AlmaLinux dir1/RHEL-based.txt:Red Hat Enterprise Linux dir2/Debian-based.txt:Linux Mint
Closing Thoughts
In this guide, we saw various command examples for grep, egrep, fgrep, and rgrep on Linux. At their core, these commands are just used to search for certain string patterns in one or more files. As you’ve seen from the examples here, their functionality can be easily extended, and applied to many useful scenarios.