DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Working with files and directories

Finding files

To search the system for a particular file, use the find(C) command, as follows:

find start_point -follow -name filename -print

The start_point argument tells find where to start searching in the filesystem, for example, root. find searches its starting directory, and all the subdirectories. If you know your file is in one of your own subdirectories you could tell find to start searching from $HOME.) The -follow option tells find that if it encounters a symbolic link, it should follow it to the file the link points to, as described in ``Navigating symbolic links''. The -name option is followed by the name of the file you are looking for. Every time find sees a file with this name, it carries out the actions specified by the subsequent options. For example, the -print option tells find that the action it must take when it finds filename is to print its pathname:

   $ find /tmp -name myfile.tmp -print
   /tmp/myfile.tmp
find gives lots of error messages when you do not have permission to search a directory, for example:
   $ find / -name chap3 -print
   /u/charles/stuff/os/chap3
   /u/w/Xenix/OS2.3.2/Intlsupp/Guide/chap3
   

find: cannot chdir to /etc/conf/pack.d/arp find: cannot chdir to /etc/conf/pack.d/arpproc . . .

To suppress these error messages, redirect the error output of the find command to /dev/null (the UNIX system's ``black hole'' directory), as follows:
   $ find / -follow -name chap3 -print 2> /dev/null
For more information on redirecting output, see ``Specifying command input and output''.

find can be used to apply a command to a collection of files that match some selection test, for example, files that are older than a specified age. You can remove all files in your home directory, and all its subdirectories, that have not been accessed for seven days by typing the following command line:

   $ find $HOME -follow -name '*' -atime +7 -exec rm {} \;
find starts from the directory specified by its first parameter (in this case, the value of $HOME), follows symbolic links, and selects all the files matching the designated name (in this case, '*') that were last accessed (-atime) seven or more days ago. It then executes (-exec) the rm command on the found file (represented in the expression by {}). The ``\;'' at the end of the line terminates the -exec expression. Note that the single quotes around the ``*'' are required. Otherwise find searches for files with names matching those matched by ``*'' in the current directory.

The -exec option allows the execution of any legal shell command along with any permitted options and arguments.

find can carry out other tasks when it finds a file. For example, the following command causes find to execute cp on any file called datafile in the directory /bin; this file is then copied to your home directory.

   $ find /bin -follow -name datafile -exec cp {} $HOME \;

Next topic: Retrieving deleted files
Previous topic: Searching for text in a file

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003