UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 23.19 Deleting (BSD) Manual Pages that Aren't Read Chapter 23
Removing Files
Next: 23.21 Removing Every File but One
 

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:

% find . -atime +60 -ok rm -f {} \;

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 find 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 ~), but leaves all others alone:

% find . \( -name core -o -name "*~" \) -atime +60 -ok rm -f {} \;

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:

% find ~ \( -name "*~" -o -name core \) -atime +1 -exec rm {} \;

But still: automated deletion commands make me really nervous, and I'd prefer to live without them.

- ML


Previous: 23.19 Deleting (BSD) Manual Pages that Aren't Read UNIX Power ToolsNext: 23.21 Removing Every File but One
23.19 Deleting (BSD) Manual Pages that Aren't Read Book Index23.21 Removing Every File but One

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System