2. Useful Tools

The fastest way to find files is with the locate command. locate is uses a database which is updated every night. This makes it very fast, but it will not find files which have been created the same day. The easiest way to use locate is just to specify a fragment of the file name, for example:

locate foo

For trimming down the results of your search, grep is invaluable. For example, to find all files which contain both report and eric in the complete file name (including path) we can use:

locate report | grep eric

If you know anything about "regular expressions", then you can do a more specific search. A good intro (in fact probably as much information as you'll ever need) to regular expressions can be found at A Tao of Regular Expressions.

If you want to specify more than the file name, for example the date modified or the file size, then the find command is the one to use. find actually searches the disk, so it can be a little bit slower. Here is an example of searching for files which are located in the directory /home/eric/misc and all subdirectories, which start with report, which have been modified in the last 2 days, and which are at least 10kb in size:

find /home/eric/misc -name 'report*' -mtime -2 -size +10k

If you want to find files by their contents, this can be done by combining a number of Linux tools together. (Combining several simple tools to do one complicated job is the Unix approach, and it is very powerful once learned.) We will use find to construct a list of files, and grep to look for lines containing a certain word. We will combine them with a pipe (|) and the command xargs which takes the output from one command and uses it as an argument for calling a second command. This sounds complicated, but the example is not too bad:

find -type f | xargs grep -i report

Here we are looking for files in the current directory (and subdirectories) which contain the word "report". The -type f flag ensures that we only get a list of regular files (and not directories and links for example) to pass to grep. The -i flag means ignore case, so we are looking for "Report" as well as "report". If you might have filenames with spaces or other special characters, this won't work quite as expected. In this case we must use:

find -type f -print0 | xargs -0 grep -i report

There is a really good tutorial on finding files in the info pages. If you are using the Konqueror web browser or the Galeon web browser, you can read it by entering info:/find in the address bar. Otherwise type info find on the command line.