How to find and remove old unused files in unix

This howto will work for any unix based operating system, such as Linux, and FreeBSD

Files can be abandon from old unused programs, and upgrades. They can be tricky to find when they are place in common directories such as /usr/share, /usr/local/lib, /bin, and so on. the find command can be used to identify files that haven't been accessed in a long time.

Caution Warning

 * This may cause some programs to not work, and possibly the whole OS.
 * Use carefully and be certain that you wish to delete all files listed
 * directories like /usr/include contain files that are not accessed often, yet are required to future program installs.

Steps

 * Finding the Files: The find command has an option for displaying file older than 'X' days from when it was last accessed, by the flag '-atime +X'.
 * In front of the 'X' the (+) means greater than, (-) means less than, and without anything means exactly equal to.
 * Format: find {directory to search in} -atime {+,-, }{number of days}
 * ​Example: find /usr/local/lib -atime +265
 * this would search in /usr/local/lib for any files older than 265 days. find is recursive so it will also look in the subdirectories of /usr/local/lib
 * If the list is very long you may want to direct the output to a file. Add '> outputfile.txt' to the end of the command and execute it.


 * Removing the ALL files listed: Using 'rm' and the '`' key you can send the file locations outputted from your command to the 'rm' command line. The '`' is the key above the 'Tab' key, and next to the '1' key on standard US PC keyboards.


 * Format: rm `find {directory to search in} -atime {+,-, }{number of days}`
 * ​Example: rm `find /usr/local/lib -atime +265`
 * ​Be sure you want to delete all the files listed. On some configurations you may have to use 'rm -f' so you don't have to confirm each file.

Remark: although the above example works most of the time, it doesn't work for large amounts of files, failing with the error: "rm: too many arguments". That's because the whole list of filenames is fed at once to rm. A better solution in this case is to use the 'internal' exec-switch to the find-command.
 * Example: find /usr/local/lib -atime +265 -exec rm {}\;

The {} represent the object found (i.e. a filename, one at a time), the \; is needed as the delimiter of the exec command.

Further Remark: The simplest option is to use the -delete flag
 * Example: find /usr/local/lib -atime +265 -delete