Finding things with grep

In this HOWTO, I'm going to start with the basics of grep, then build onto more advanced concepts in later HOWTO's. First, for those that may be unfamiliar with the term, "grep" represents "global regular expression print". The utility derived from ed, a command line editor on Unix and is used to print all lines matching a pattern.

This HOWTO assumes you have at least a basic level of understanding in Linux and have seen the command line interface before. As many have attested in the past, the tools available to people within Linux really show their teeth on the command line. In another article, I talked about finding things with the find command. grep can be thought of as a similar tool in that it can also find things, but grep can be more granular.

In this example, we're going to use grep to find anything in a file that matches what we tell grep to look for. First, it's worth noting that grep, by itself, with no options, will search using case sensitive syntax. So, if I type grep john /etc/passwd and, within the passwd file, John exists, but not john, grep will not display any output. This is as designed. To search without case sensitivity, use the -i option: grep -i john /etc/passwd

To view all options that can be used with grep, use the man pages. Ex. man grep

So, to use another example, let's put grep to use in a real-world scenario.

Suppose I need to find errors in my logs on a Linux system. Here's an example:

grep -i error /var/log/messages

Any entry in the messages file that's "error" or "Error" will get displayed. If the output flies past you because there's more than one screenful of output, you can pipe grep to less like this:

grep -i error /var/log/messages | less

This tells the system to display output one screen at a time. In case you haven't heard of pipe, yet, this is simply a way to take the output of one command and pipe, or send that to another command.

Since the previous example is a long command and maybe you want to run this often enough to keep from having to type it out every time, you may want to look at creating an alias for that command. We'll cover aliases next.

To view or add a comment, sign in

More articles by John Holmes

  • Firewalld Notes

    This information is used as my own personal reference. If you should find it useful, I’m glad of it.

    1 Comment
  • Firewalld Notes

    This information is used as my own personal reference. If you should find it useful, I’m glad of it.

  • Jenkins upgrade 1.x to 2.x

    Just finished successfully upgrading Jenkins on a CentOS server running 6.4.

    1 Comment
  • Systemd Cheatsheet

    Like it or not, systemd is the new way of doing things as of CentOS 7. In an attempt to help folks get used to the new…

    3 Comments
  • All about aliases

    The alias command allows you to store commonly used commands into a pre-set string of characters. For this example…

  • Fun with the find command

    Over the years, I've found some pretty useful find recipes to get the job done in a quick hurry. I thought I'd provide…

    2 Comments

Explore content categories