Finding Text Within Files on a Linux/Unix System

This simple task can be accomplished with the following command:

grep -H -i -r -I -l 'The text you are looking for' /full/path/to/file

Explanation

The following descriptions for each parameter are paraphrased from the man pages for the grep command.

Parameter What the Parameter means
-H Displays the filename for each match.
-i Ignore the case of the text in the text you are looking for as well as in the files that are being searched.
-r Recursively read all files in each directory.
-I Skip over any binary files. grep guesses which files are binary and which are not.
-l Display the filename for each file which contains a match and stop scanning the file after the first match is found. It is unclear how this ties in with the -H parameter.

Examples

grep -H -i -r -I -l 'workflow' /www/htdocs/*
Perform a case-insensitive search for all files in the /www/htdocs directory containing the text workflow.
grep -H -i -r -I -l 'workflow' /www/htdocs/* > ~phajjar/myFile.txt
Perform a case-insensitive search for all files in the /www/htdocs directory containing the text workflow and save the output to text file named myFile.txt located in phajjar's home directory.

Caveats

A file will be skipped if the account under which you execute this command does not have read permissions for that file. The same goes with directories. In either situation you will receive an error message stating such. This should not stop the search altogether though.

Disclaimer

Try to be careful when running this as root.

This quick and dirty example does not even begin to scratch the surface of the grep command. You can throw nasty things like regexps and other such tools, but that's way beyond the scope of this little article.

I'm also aware of the fact that this task on its own is not really a "stupid networking trick" but since I was doing it in the context of performing such a trick, it counts as such!


Back to Stupid Networking Tricks philatfacebook@gmail.com

Valid HTML 4.01 Transitional