Skip to main content
Skip table of contents

Using grep to filter results

The command line utility “grep” is one of the most powerful and useful tools in Linux. Its most common use is to filter results from everyday commands. For instance, if you want to see all the hostnames your system has mapped out in /etc/hosts you can simply run:

CODE
$ cat /etc/hosts

But if you know you’re just looking for a specific hostname, you can pipe the output through grep:

CODE
$ cat /etc/hosts | grep head
 10.3.1.254   head-ib  head-ib.cluster
 10.2.1.254   head-mgmt  head-mgmt.cluster
 10.2.1.253   head-ipmi  head-ipmi.cluster
 10.1.1.254   head  head.cluster

This limited the search results to lines that contained the word “head,” which represents this cluster’s head node.

Another very useful grep technique is to find every instance of a word or pattern within every file in a directory or even sub-directories:

CODE
$ grep -R “words to search for” /path/to/search/in/

This will scan every file within the path you specify and find any instance of the word or words you listed.

These two examples are just the beginning of what you can do with grep. To get the full manual for the grep command, simply type:

CODE
$ man grep
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.