Skip to main content Skip to navigation

3.1.2 Finding files

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 . 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?

Another way of finding files by name is to use the locate command. The command syntax is

locate string

where string can be the filename, or just part of the filename (and you don't need to 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?