Grep is the number one tool used by linux systems administrators. I rely on this tool everyday, but its used in many different ways. I decided to list my top six uses.
Top use is grep with the recursive switch (grep -r). This is used for searching a particular filesystem path for a particular word. One common reason why I use this switch is when Im searching in a nagios configuration directory for mentions of a hostname. This allows me to find all the locations where a hostname has been mentioned and I can then alter it if needed.
1 |
grep -r expression /path |
This straightforward search allows you to look in a text file and find all the locations where the expression or search word can be found. The results will include the entire line where the expression has been matched.
1 |
grep expression /filename |
The will do the opposite from he previous command, this will exclude all mentions of the search criteria. This will remove the entire line from the search results.
1 |
grep -v expression /filename |
This will match the line but also include a certain number of subsequent lines, the number indicates how many lines after the match you would like included.
1 |
grep -A 1 expression /filename |
Again this the opposite of the previous command, this will include a certain number of lines before the match line.
1 |
grep -B 1 expression /filename |
This also proves to be very useful, this will remove case sensitivity from your search string.
1 |
grep -i expression /filename |