|
|
You can use the find command to find a file anywhere on the system.
find / -name afile -print
To use this command, type find, the name of the directory where you want it to start looking, -name and the name of the file you want to find, -print, then press <Enter>. For example, to look for a file called rts starting in the directory /etc, you could type:
$ find /etc -name rts -print
/etc/perms/rts
The find command in the preceding example says,
``Find a file named rts, and print the pathname when you find it.
Start looking in the directory /etc.''
The find command starts from the directory you specify
and looks through every directory below it for files with names
that match the file you put after -name.
If find runs across directories where you do not have read permission,
it gives you an error message like
find: cannot open
directoryname.
If you are trying to find something starting from the root
directory (/), which could take some time, you may want to redirect
the output to a file and put the whole task in the background:
find / -name mytodo -print > foundfile &
The find command has many options in addition to what you have seen here.
Using find, you can locate a file based on any of its attributes, for example, its owner, its size, or the time it was last modified. You can also tell find to perform a particular command when it finds a file; for example, you can have find look for all files older than a certain date and remove them.
Because find produces output containing absolute pathnames of everything it found, find can be a useful beginning of a pipeline anytime you need to generate a lot of pathnames. For example, system administrators who use the cpio(C) backup program use find to generate a list of files to be backed up, and then they pipe the find output through cpio to do the actual backing up. See find(C) for information about all the find options.