, , ,

The Linux command “grep” is so useful and simple, if you’re not sure what it does you should take two minutes to have a look.

Essentially it allows you to search a line of text for a particular string and print out the line if a match is found. You could use grep to look through text files, log files or datasets to look for relevant lines. For example, if you have a log file of web server access you could use something like:

grep 404 access.log

to find lines that include 404. Obviously this is overly simplified but you get the idea. Grep can take regex expressions as well so you could search for log events that occur during a specified time period.

Using the pipe | operator you can pass the output of one operation into grep for further processing. For example, searching the file system. To find all places in the file system that have the php.ini file.

find / | grep php.ini

To find processes with the name mysql:

ps -A | grep mysql

In short grep allows you to filter any list of text down to just the lines you’re interested in.

Grep man page