12 Practical Examples of Linux grep Command

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

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.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

20 Responses

  1. willam Smith says:

    I hate Linux and we should ban it

  2. kl says:

    SUDO means Super User DO, we may not have privileges to execute a command. at that time we will use sudo to get the job done. :)

  3. Jim says:

    To count all occurrences of a pattern, try

    ifconfig | grep -o bytes | wc -l

    Compare the output against

    ifconfig | grep -c bytes

  4. beginner says:

    i think the example which u provided is not full proof which u mentioned in your point

    6. Count Number of Matches

    ifconfig | grep –c inet6

    this will only give the first occurrence of the word/pattern in every line and not all the matches.

    Can you please provide any other solution ??


  5. Rob Krul says:

    I’m not sure why `sudo` is in there. I didn’t write the article that way, nor would I ever want anyone to use sudo on files if they don’t have to. Must have gotten inadvertently messed up when it went to print. I will contact the editors immediately.

  6. vasilisc says:

    much better

    sudo grep –vE “(#|^$)” /etc/apache2/sites-available/default-ssl

    remove blank line and comment

  7. iPenguin says:

    Why are you using sudo in all your examples?

  8. Darren says:

    nice list of practical uses of ‘grep’, I might use it in my class, however why are all the commands run with ‘sudo’? None of the commands listed should require ‘sudo’ to run properly (unless of course the user does not have permission to read/list the files in question, but that is beyond the scope of this article).

  9. mesuutt says:

    Good tips. Thanks @Rob.

  10. joni says:

    Apache config example (grep -v “#”) is a little dangerous. Unexpected will happen if a line contains # but does not start with it.

    Better could be grep -v ” *#”

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *