23.20 Deleting Stale Files
Sooner or later, a lot of junk collects in your directories: files that you don't really care about and never use. It's possible to write find ( 17.1 ) commands that will automatically clean these up. If you want to clean up regularly, you can add some find commands to your crontab file ( 40.12 ) .
Basically, all you need to do is write a find command that locates files based on their last access time ( -atime ( 17.5 ) ), and use -ok or -exec ( 17.10 ) to delete them. Such a command might look like this:
This locates files that haven't been accessed in the last 60 days, asks if you want to delete the file, and then deletes the file. (If you run it from cron , make sure you use -exec instead of -ok ; and make absolutely sure that the find won't delete files that you think are important.)
Of course, you can modify this
command to exclude (or
select) files with particular names; for example, the command below
deletes old core dumps and GNU Emacs backup files (whose names end in
If you take an automated approach to deleting stale files, here are some things to watch out for:
OK, I've said that I don't really think that automated deletion scripts are a good idea. What's my solution, then?
I don't have a good comprehensive solution. I spend a reasonable amount of time (maybe an hour a month) going through directories and deleting stale files by hand. I also have a clean alias that I type whenever I think about it. It looks like this:
alias clean "rm *~ junk *.BAK core #*"
That is, this alias deletes all of my Emacs ( 32.1 ) backup files, Emacs autosave files (I admit, that's risky), files named junk , some other backup files, and core dumps ( 52.9 ) . I'll admit that since I never want to save these files, I could probably live with something like:
But still: automated deletion commands make me really nervous, and I'd prefer to live without them.