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:

$ cat /etc/hosts

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

$ cat /etc/hosts | grep head   head-ib  head-ib.cluster   head-mgmt  head-mgmt.cluster   head-ipmi  head-ipmi.cluster   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:

$ 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:

$ man grep