12 Practical Examples of Linux Grep Command

Have you ever been confronted with the task of looking for a particular string or pattern in a file, yet have no idea where to start looking? Well then, here is grep to the rescue!

grep is a powerful file pattern searcher that comes equipped on every distribution of Linux. If for whatever reason, it is not installed on your system, you can easily install it via your package manager as shown.

$ sudo apt install grep         [On Debian, Ubuntu and Mint]
$ sudo yum install grep         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a sys-apps/grep  [On Gentoo Linux]
$ sudo apk add grep             [On Alpine Linux]
$ sudo pacman -S grep           [On Arch Linux]
$ sudo zypper install grep      [On OpenSUSE]    

I have found that the easiest way to get your feet wet with the grep command is to just dive right in and use some real-world examples.

1. Search and Find Files in Linux

Let’s say that you have just installed a fresh copy of the new Ubuntu on your machine and that you are going to give Python scripting a shot.

You have been scouring the web looking for tutorials, but you see that there are two different versions of Python in use, and you don’t know which version of Python is installed on Ubuntu by the installer, or if it installed any modules.

Simply run the following dpkg command with grep as shown:

# dpkg -l | grep -i python
Finding Files with Grep Command
ii  libpython3-stdlib:amd64                    3.8.2-0ubuntu2                      amd64        interactive high-level object-oriented language (default python3 version)
ii  libpython3.8:amd64                         3.8.10-0ubuntu1~20.04.5             amd64        Shared Python runtime library (version 3.8)
ii  libpython3.8-minimal:amd64                 3.8.10-0ubuntu1~20.04.5             amd64        Minimal subset of the Python language (version 3.8)
ii  libpython3.8-stdlib:amd64                  3.8.10-0ubuntu1~20.04.5             amd64        Interactive high-level object-oriented language (standard library, version 3.8)
ii  python-apt-common                          2.0.0ubuntu0.20.04.8                all          Python interface to libapt-pkg (locales)
ii  python3                                    3.8.2-0ubuntu2                      amd64        interactive high-level object-oriented language (default python3 version)
ii  python3-apport                             2.20.11-0ubuntu27.24                all          Python 3 library for Apport crash report handling
ii  python3-apt                                2.0.0ubuntu0.20.04.8                amd64        Python 3 interface to libapt-pkg
ii  python3-aptdaemon                          1.1.1+bzr982-0ubuntu32.3            all          Python 3 modules for the server and client of aptdaemon
ii  python3-aptdaemon.gtk3widgets              1.1.1+bzr982-0ubuntu32.3            all          Python 3 GTK+ 3 widgets to run an aptdaemon client
ii  python3-blinker                            1.4+dfsg1-0.3ubuntu1                all          fast, simple object-to-object and broadcast signaling library
ii  python3-brlapi:amd64                       6.0+dfsg-4ubuntu6                   amd64        Braille display access via BRLTTY - Python3 bindings
...

First, we ran dpkg –l, which lists installed *.deb packages on your system. Second, we piped that output to grep –i python, which simply states “go to grep and filter out and return everything with ‘python’ in it.”

The –i option is there to ignore-case, as grep is case-sensitive. Using the –i option is a good habit of getting into unless, of course, you are trying to nail down a more specific search.

2. Search and Filter Files in Linux

The grep can also be used to search and filter within individual files or multiple files. Let’s take this scenario:

You are having some trouble with your Apache Web Server, and you have reached out to one of the many awesome forums on the net asking for some help.

The kind soul who replies to you has asked you to post the contents of your /etc/apache2/apache2.conf file. Wouldn’t it be easier for you, the guy helping you, and everyone reading it if you could remove all of the commented lines? Well, you can! Just run this:

# grep -v ^\# /etc/apache2/apache2.conf | grep  .

The –v option tells grep to invert its output, meaning that instead of printing matching lines, do the opposite and print all of the lines that don’t match the expression, in this case, the # commented lines.

Note that we also used grep . at the end in order to hide the output of all empty lines. This way we only see the configuration settings in our terminal.

Print File Content By Excluding Comments and Blank Lines
DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
	Options FollowSymLinks
	AllowOverride None
	Require all denied
</Directory>

3. Find all .mp3 Files in Linux

The grep can be very useful for filtering from stdout. For example, let’s say that you have an entire folder full of music files in a bunch of different formats.

You want to find all of the *.mp3 files from the artist JayZ, but you don’t want any of the remixed tracks. Using a find command with a couple of grep pipes will do the trick:

# find . –name “*.mp3” | grep –i JayZ | grep –vi “remix”

In this example, we are using find to print all of the files with a *.mp3 extension, piping it to grep –i to filter out and print all files with the name “JayZ” and then another pipe to grep –vi which filters out and does not print all filenames with the string (in any case) “remix”.

[ You might also like: 35 Practical Examples of Linux Find Command ]

4. Display the Number of Lines Before or After the Search String

Another couple of options are the –A and –B switches, which display the matched line and number of lines either that come before or after the search string.

While the man page gives a more detailed explanation, I find it easiest to remember the options as –A = after, and –B = before:

# ifconfig | grep -A 4 inet
# ifconfig | grep -B 2 UP
Print Number of Lines Before and After String
Print Number of Lines Before and After String

5. Prints Number of Lines Around Match

The grep’s –C option is similar, but instead of printing the lines that come either before or after the string, it prints the lines in either direction:

# ifconfig | grep -C 2 lo

 TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 15314  bytes 1593769 (1.5 MB)
        RX errors 0  dropped 0  overruns 0  frame 0

6. Count the Number of Matches

Similar to piping a grep string to word count (wc command) grep’s built-in option can perform the same for you:

# ifconfig | grep -c inet6

7. Search Files by Given String in Linux

The –n option for grep is very useful when debugging files during compile errors. It displays the line number in the file of the given search string:

# grep -n "main" setup.py

8. Search a string Recursively in all Directories

If you would like to search for a string in the current directory along with all of the subdirectories, you can specify the –r option to search recursively:

# grep -r “function” *

9. Search for the Entire Pattern

Passing the -w option to grep searches for the entire pattern that is in the string. For example, using:

# ifconfig | grep -w "RUNNING"

Will print out the line containing the pattern in quotes. On the other hand, if you try:

# ifconfig | grep -w "RUN"
Find Entire Pattern
Find Entire Pattern

Nothing will be returned as we are not searching for a pattern, but an entire word.

10. Search a string in Gzipped Files

Deserving some mention are grep’s derivatives. The first is zgrep, which, similar to zcat, is for use on gzipped files. It takes the same options as grep and is used in the same way:

# zgrep -i error /var/log/syslog.2.gz
Search String in Gzipped Files
Search String in Gzipped Files

11. Match Regular Expressions in Files

The egrep command is another derivative that stands for “Extended Global Regular Expression”. It recognizes additional expression meta-characters such at + ? | and ().

[ You might also like: What’s Difference Between Grep, Egrep, and Fgrep in Linux? ]

egrep is very useful for searching source files, and other pieces of code, should the need arise. It can be invoked from regular grep by specifying the –E option.

# grep -E

12. Search a Fixed Pattern String

The fgrep searches a file or list of files for a fixed pattern string. It is the same as grep -F. A common way of using fgrep is to pass a file of patterns to it:

# fgrep –f file_full_of_patterns.txt file_to_search.txt

This is just a starting point with grep, but as you are probably able to see, it is invaluable for a variety of purposes. Aside from the simple one-line commands we have implemented, grep can be used to write powerful cron jobs, and robust shell scripts, for a start.

[ You might also like: 11 Advanced Linux ‘Grep’ Commands on Character Classes and Bracket Expressions ]

Be creative, experiment with the options on the man page, and come up with grep expressions that serve your own purposes!

If you read this far, tweet to the author to show them you care. Tweet a thanks
Rob Krul
Rob is an avid user of Linux and Open Source Software, with over 15 years experience in the tech geek universe. Aside from experimenting with the many flavors of Linux, he enjoys working with BSDs, Solaris, and OS X. He currently works as an Independent IT Contractor.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

22 thoughts on “12 Practical Examples of Linux Grep Command”

  1. The example number 2 is is VERY WRONG.

    # grep -v “#” /etc/apache2/sites-available/default-ssl
    

    will also exclude lines like: value display_errors off; # turns off errors right way to this is:

    # grep -v "^#"  /etc/apache2/sites-available/default-ssl
    
    so it will exclude only lines that begins with #.
    Reply
  2. Im new to grep, but i have found it very handy.

    I do allot of searching the same files every day and most of the time its a single file
    grep -i -A50 “diskview -j” *-Report* | less -R

    i have this alias that works great thought sometimes i am working on several systems at once and they all have the same file.
    I need to know what array i am looking at but i dont need it to print the file name for all 50 lines.

    So what i was trying to figure out is can i do a grep -i -l “diskview -j” *-Report*

    followed by my original grep command so i get the file name by it self followed by the diskview -j and the next 50 lines?

    Or is this going to produce 3 file names followed by 3 diskveiw -j reports?

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.