How to Search for Only Text Files

Files on a large dedicated server are difficult enough to find on their own.  When you add the fact that numerous files may have the same or similar names but be of different types, the odds of finding what you want may seem insurmountable.  Fortunately, there is a tool in Linux for just about anything.  Using the “find” command and a few expressions in the command string, you can weed out all of the binary files and anything else, leaving only plain text files.  This technique is useful particularly when searching a dedicated server for scripts, legitimate or otherwise.

From within SSH, type the following string:

$ find vhost -type f -exec grep -l "php execute" {} \; -exec file {} \; | grep text | cut -d ':' -f1

This will sift through those pesky binary files in the “vhost” directory and leave only the text ones containing the words “php execute”.  The benefit of this is that you save valuable CPU cycles by not searching through binary files, which can be quite large, and the vhost directory or any other directory associated with your web server, can be rather large.  It works by searching only the files with the mime type “text”.

For more information about the “find” command, you can type “man find” from the command line.  Special thanks to Kavoir for the search tips.











Comments: