Basic grep commands

I'm including this as a faq-lette for those attempting to migrate websites that have been written with silly stuff like full server path references :-)

Usage:

grep [options] [string] [file]

You can use grep recursively to search for strings in all files in all folders and subfolders.  An example:

grep -r "this string" *

This simple command will return a list of each file that contains "this string" and display the line number where it resides.

Using:

grep -lr "this string" *

will just return a list of files that contain "this string"

Wildcards work within the regular expression, so:

grep -lr "this st*" *

will return a list of files containing "this" followed by any word starting "st", eg: "this straw", "this strange", "this stream".

Use grep recursively to find multiple strings, handy if you want, for example, a list of files linking to documents with an .htm extension, or .html.

grep -r "htm\|html\|shtml" *

You can refine your search to certain filetypes:

grep -lr "this string" ./log

will return a list of files starting with the name 'log' that contain "this string"

WARNING running long complex searches can be very resource hungry - use with care!

More stuff on Linux Info (linfo) here