Next: , Previous: Archiving, Up: Common Tasks



4.3 Cleaning Up

This section gives examples of removing unwanted files in various situations. Here is a command to remove the CVS backup files created when an update requires a merge:

     find . -name '.#*' -print0 | xargs -0r rm -f

The command above works, but the following is safer:

     find . -name '.#*' -depth -delete

You can run this command to clean out your clutter in /tmp. You might place it in the file your shell runs when you log out (.bash_logout, .logout, or .zlogout, depending on which shell you use).

     find /tmp -depth -user "$LOGNAME" -type f -delete

If your find command removes directories, you may find that you get a spurious error message when find tries to recurse into a directory that has now been removed. Using the -depth option will normally resolve this problem.

To remove old Emacs backup and auto-save files, you can use a command like the following. It is especially important in this case to use null-terminated file names because Emacs packages like the VM mailer often create temporary file names with spaces in them, like #reply to David J. MacKenzie<1>#.

     find ~ \( -name '*~' -o -name '#*#' \) -print0 |
       xargs --no-run-if-empty --null rm -vf

Removing old files from /tmp is commonly done from cron:

     find /tmp /var/tmp -not -type d -mtime +3 -delete
     find /tmp /var/tmp -depth -mindepth 1 -type d -empty -delete

The second find command above uses -depth so it cleans out empty directories depth-first, hoping that the parents become empty and can be removed too. It uses -mindepth to avoid removing /tmp itself if it becomes totally empty.