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 (Section 9.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 ( Section 25.2).
Basically, all you need to do is write a find command that locates files based on their last access time (-atime (Section 9.5)) and use -ok or -exec (Section 9.9) 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 following
command 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, watch out for these things:
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. But 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:
!
Section 9.6, -perm
Section 9.15
% 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. To archive the
deleted files on tape, you can use the find
-cpio
operator if your system has it. Otherwise, try a little
shell script with GNU tar; the following script writes the list
of files to a temporary file and then, if that succeeds, reads the list
of files, writes them to tape, and removes the files if the tape write
succeeds:
umask 077 files=/tmp/CLEANUP$$
if
Section 35.13, &&
Section 35.14
if find ... -print > $files
then tar -c -T $files --remove && rm $files else echo "cleanup aborted because find returned nonzero status" fi
Okay, I've said that I don't really think that automated deletion scripts are a good idea. However, 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 (Section 19.1) backup files, Emacs autosave files (risky, I know), files named junk, some other backup files, and core dumps. I'll admit that since I never want to save these files, I could probably live with something like this:
% 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