skip to content

Finding Files

If you know that a file is somewhere in your home directory / data space / scratch space but you don't know exactly where, Linux can look for it for you. If the file turns out to have been deleted rather than simply mislaid, we may be able to restore it from backup.

Searching by name

The quickest way is to use the local hlocate command, which will find a file in your home directory given all or part of its name. E.g. hlocate thesis will find all files in your home directory and its subdirectories which have the word "thesis" in their names. (Remember, Linux filenames are case-sensitive so a file called "Thesis" will not be found by this command.)

hlocate works by running the standard Unix locate command, but reading a database of the individual user's files rather than of all the files on the local filesystem. While it is very fast, it has a couple of limitations. It cannot find files in scratch and data spaces and it cannot find files which were created after its nightly update.

A more general Unix command for searching for files is find which looks through the directory tree. The command to find all files in the current directory and its subdirectories which have the word "thesis" in their names is:

find . -name "*thesis*" -print

find is a versatile command which can search for more properties of a file than just its name. Type man find for details.

Searching by content

If you can't remember the name of the missing file but can remember a "key word" which it contained, then the command grep will come in handy. E.g. to list all files in the current directory which contain the word "Internet":

grep -l Internet *

(without the -l it will list all the lines containing the word as well as the filenames). If the file you're looking for could be in a subdirectory, you can combine the find and grep commands like this:

find . type -f -print | xargs grep -l Internet