home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


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:

  • There are plenty of files (for example, UNIX utilities and log files) that should never be removed. Never run any "automatic deletion" script on /usr or / or any other "system" directory.

  • On some systems, executing a binary executable doesn't update the last access time. Since there's no reason to read these files, you can expect them to get pretty stale, even if they're used often. You don't want to delete them. If you cook up a complicated enough find command, you should be able to handle this automatically. Something like this should (at least partially) do the trick:

    
    
    !
     
    -perm
     
    
    % 
    
    find . -atime +30 ! -perm -111 ... -exec rm {} \;
    
    

  • Along the same lines, you'd probably never want to delete C source code, so you might modify your find command to look like this:

    % 
    
    find . -atime +30 ! -perm -111 ! -name "*.c" ... -exec rm {} \;
    
    

  • I personally find that automatically deleting files is an extreme and bizarre solution. I can't imagine deleting files without knowing exactly what I've deleted or without (somehow) saving the "trash" somewhere just in case I accidentally removed something important. You can use the find -cpio operator (if your system has it) to archive ( 19.9 ) the deleted files on tape.

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 Tools Next: 23.21 Removing Every File but One
23.19 Deleting (BSD) Manual Pages that Aren't Read Book Index 23.21 Removing Every File but One

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