Find a file containing a text string on Linux

There is a very useful command in Linux, called grep. It is a utility for searching plain text files using regular expressions. You can either search files and folders as a parameter, or pipe command outputs to it, such as grep’ing a tail.

For looking for a file:

grep -r "string to find" /folder/to/look/in/

Thats the simple answer.

If you want to pipe the grep to monitor say a tail

tail -f /var/log/maillog | grep "chris@tatedavies.com"


Read the last x lines of a log file

If like me you need to check out the PHP error log, but its bloody massive, you might want to take the last few lines of it.

tail -x log_file > new_log

That will take the last x number of lines from log_file and save into new_log

tail -10000 /var/log/httpd/error_log > ~/short_error_log

The above example will take the last 10,000 lines of the Apache error log, and save it into a new file ‘short_error_log’ in your home folder.