Next | Prev | Up | Top | Contents | Index

Locating Unused Files

Part of the job of cleaning up filesystems is locating and removing files that have not been used recently. The find command can locate files that have not been accessed recently.

The find command searches for files, starting at a directory named on the command line. It looks for files that match whatever criteria you wish, for example all regular files, all files that end in .trash, or any file older than a particular date. When it finds a file that matches the criteria, it performs whatever task you specify, such as removing the file, printing the name of the file, changing the file's permissions, and so forth.

For example:

# find /usr -local -type f -mtime +60 -print > /usr/tmp/deadfiles & 
In the above example:

/usr

specifies the pathname where find is to start.

-local

restricts the search to files on the local system.

-type f

tells find to look only for regular files and to ignore special files, directories, and pipes.

-mtime +60

says you are interested only in files that have not been modified in 60 days.

-print

means that when a file is found that matches the -type and -mtime expressions, you want the pathname to be printed.

> /usr/tmp/deadfiles &


directs the output to the temporary file /usr/tmp/deadfiles and runs in the background. Redirecting the results of the search in a file is a good idea if you expect a large amount of output.
As another example, you can use the find command to find files over 7 days old in the temporary directories and remove them. Use the following commands:

# find /var/tmp -local -atime 7 -exec rm {} \; 
# find /tmp -local -atime 7 -exec rm {} \; 
This example shows how to use find to locate and remove all core files over a week old:

# find  / -local -name core -atime +7 -exec rm {} \; 
See the cron(1M) reference page for information on using the cron command to automate the process of locating and possibly removing.


Next | Prev | Up | Top | Contents | Index