28-09-2010

BASH use grep to find files containing text

You can use GNU grep to search a whole directory and print out all files containing a certain string. Very handy indeed. Here's an example

grep -r "search string" /tmp

This will search all files in the /tmp directory for the specified string and print out any lines matching it.

To also print a few lines before or after the matching lines you can use the B (before) and A (after) parameters.

The following example finds lines containing 'facebook' in a file called 'server.log' and prints out any five lines preceding and following it:

grep facebook -B 5 -A 5 server.log

Comments:

Your comment:

»

 

[x]