|
|
You can locate all files with a specified
name,
permissions setting,
size,
type,
owner,
or last access or modification date using
find(1).
Use this command to locate seldom-used or excessively large files,
files owned by a particular user,
temporary files, core files, and any unused a.out files.
The syntax of find is:
find pathname option
pathname is the pathname of the directory to search. The find command searches recursively, downward through all the directories under the named directory, for files that match the criteria specified by option.
See also:
Use find with the -name option to locate temporary files for removal.
A temporary file contains data created as an intermediate step during execution of a program. This file may be left behind if a program contained an error or was prematurely stopped by the user. The name of a temporary file depends on the program that created it. In most cases, the user has no use for temporary files, and you can safely remove them.
Use find to locate files with a specific name, such as temp.
For example, to locate and display all files named temp
under the /usr directory, enter:
find /usr -name temp -print
When searching for temporary files,
it is a good idea to search for files that have
not been accessed for a reasonable period of time.
For example, to find all temp files in the /usr
directory that have not been accessed for one week
(-atime +7), enter:
find /usr -name temp -atime +7 -print
Once you locate the temp files, you can remove them automatically using the -exec option to find. See ``Executing commands based on find output''.
You can execute a specific shell command on the files that find locates using the -exec option. The most common use of -exec is to locate a group of files and then remove them.
For example, to find all the core files in the /usr
filesystem that have not been accessed in seven days
and remove them, enter:
find /usr -name core -atime +7 -exec rm "{}" \;
As another example, when you retire a user,
use find to locate all the files owned by that user,
back them up, and then remove them from the system.
To do this, enter:
find / -user edwarda -print | cpio -ovBc > /dev/rfd0
find / -user edwarda -exec rm "{}" \;
The first command locates all the files owned by user edwarda and copies the files to a floppy disk archive. The second command locates the files and then removes them. For more information on copying files to an archive, see cpio(1).
To specify that find prompt you with the command line
that find generates before executing the shell command
on each file, use -ok in place of -exec:
find / -user edwarda -ok rm "{}" \;
In this case, find prompts you with:
<rm ... /u/edwarda/billboard >?To execute the command (in this case, rm), enter y. If you enter any character other than ``y'', the command is not executed.
Another common use of find with the -exec
option is to locate all the files that belong to a
particular group and change them.
For example, if a user changes groups, you can use find
to locate and change all their files to the new group:
find / -user edwarda -exec chgrp pubs "{}" \;
You can use find to change the owner of a group of files.
For example, if you retire a user and you want to transfer ownership of
their files to another user, use this command:
find / -user edwarda -exec chown earnestc "{}" \;
Using this construction to execute a command on a large group of files can be very slow because the -exec option forks a separate process for each file in the list. A more efficient method for doing this is to use xargs(1) in place of -exec. The xargs command forks fewer processes to execute the command on the entire group of files.
The following example illustrates how to use the xargs
construction with find:
find / -user edwarda -print | xargs chown earnestc
This command accomplishes the same thing as the previous example, only much more efficiently.