Let’s revisit filters with a timeless classic – grep.
grep – the search tool
One of the most common filters we employ is the mighty grep (global regular expression print). This tool searches for a regular expression (read: normal text) within files, allowing us to find which files contain the content we’re looking for. It’s much like the “search” tools on websites or within word processing programs.
The basic search contains three paramaters, in the following order:
grep “REGULAR EXPRESSION” file1 file2 …
That means that we can search for the text “REGULAR EXPRESSION” within any number of files using the grep tool.
We can replace the file names with asterisks (*) to check all files in the current directory.
grep “Regular Expression” *
We can also use grep recursively (that is, go through subdirectories in addition to the current directory) using the -R option with no file names.
grep -R “Regular Expression”
Regular expressions are a powerful tool which very few understand. This tutorial at gnosis.cx is an excellent breakdown of the topic – I highly recommend it. I will also be covering this content in the near future, but for now go to the link.
The following series of images demonstrate most of the common uses of grep. I didn’t include grep -R, because there aren’t interesting results in our subdirectories. You can play with grep -R as an exercise.
Note: In the future, we will refer to Regular Expressions as Regex (Pronounced with the hard g in “guard”, not the soft g in “giant”). This is the community abbreviation.