Skip to main content Skip to navigation

3.1.2 Finding files

In this section, you will find out more about three commands, ls, find and locate.

You have already seen how to use the command ls to get a listing of files, for any path that you specify (subject to viewing permissions being set for that path). The ls command has lots of options, you've already met a few if you've used ls -lrt (first introduced in Section 1.7 ). To find out more about using ls, type the command man ls.

Putting it into Practice:

Find out about some of the options associated with the ls command.
What option do you need to sort your listed contents by size?

On a large filesystem, you can easily lose track of where all files are kept. Instead of just looking by hand, it's preferable to use a search utility under linux called find. To search for a file by name, the general syntax is as follows

find pathname -name string

Example:

The user phillipa would like to find the file tinker - all she knows is that it is somewhere in her home filespace (however, it is located in ~phillipa/CASTEP/mythings/). She does a search by typing
 find /home/phillipa -name tinker 
The output to her shell is
 /home/phillipa/CASTEP/mythings/TIO2/tinker 


The string in the syntax above does not need to be one word. You can use the asterix (*) to do a wild-card substitution. However, in this case you must also enclose the string in quotation marks.


Example:

The user phillipa would like to find an input file ending in .inp, but she can't remember if she called it titania.inp or tio2.inp - all she knows is that it is somewhere in her home filespace (however, it is called titania.inp and is located in ~phillipa/gaussian/surface/). She does a search by typing
 find /home/phillipa -name "*.inp" 
The output to her shell is
 /home/phillipa/gaussian/surface/titania.inp 


You can also use other criteria to search for files such as how large the file is (find pathname -size +5000k to find files larger than 5MB), and much more. Find out ALL the options for the find command by using man.

Putting it into Practice:

What option do you need to find files prioritised by time of last access?


Often, the find command can give you a lot of output, so much that it just scrolls past on x-term. One way to control the output is to use a pipe (as introduced in Section 1.11).

Putting it into Practice:

In this exercise, you will search for all files under /usr/. This will yield a lot of output, so you will need to pipe this to the less command (this gives you output one page at a time, hit the space bar to advance a page down - see Section 3.1.4 for more information). To do this, type
 find /usr/ | less 

Another way of finding files by name is to use the locate command. This command uses a pre-generated database file for quickly finding the result. The database is then updated daily. Note that 'locate' is not always available. The command syntax is

locate string

where string can be the filename, or just part of the filename (and you shouldn't use wildcarding in the string).

Example:

The user phillipa would like to find a file that contains the string bro She does a search by typing
 locate bro
SOME of the output to her shell is
 /home/phillipa/gaussian/surface/broken.inp 
/usr/share/doc/mozilla-browser 
/home/phillipa/junk/bin/vibrot 
Lots more output was actually recorded. What happens if you type locate bro? (if 'locate' is installed)