find

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"

 

Finding files in Linux/Mac Terminal

Finding files is a little different to MS DOS!

find <startlocation> <type> <filename> <exec>

The exec is a simple way of performing functions on the results. If you leave it off, the find doesn’t really do anything.

So, the simplest thing to do is find a file, so the exec becomes the output:

find / -name wkhtmltopdf* -exec ls -ls {} \;

That will find all files starting with wkhtmltopdf starting from / and the function to run on it is ls -ls {} \;

The {} will be replaced with the search result, so, in essence, ls -ls filename; will show you the details about the file which is what I want, as I am looking for all copies of the wkhtmltopdf toolkit on the server.

You can obviously change the exec to delete the files, or copy/move them, orĀ interrogateĀ further…