« Kathy Sierra, Chris Locke, and Due Process | Main | Defrag Conference »
Cleaning Up Unwanted Files in Linux
One of my grad students just went to remove some unwanted, automatically created files in his directory and accidentally deleted some things he wanted. I use a script to do clean ups to prevent these kinds of silly errors (which we’re all prone to). Here’s the script:
#!/bin/bash
if [ ! -e $HOME/.rmd ]
then mkdir $HOME/.rmd
fi
find $HOME \( -name '.rmd' -prune \) -o \
\( -name '*~' \
-o -name ',*' \
-o -name '#*#' \
-o -name '*.bak'\
-o -name '*.backup' -atime +5\
-o -name 'core'\
\) \
-print -exec mv -f {} $HOME/.rmd \;
find $HOME/.rmd -atime +5 -exec rm -f {} \;
The script creates a directory called .rmd if it doesn’t exist, finds files matching a certain set of patterns to that directory, and finally removes things in that directory that were moved there more than five days ago. It’s not perfect—files with the same name are just moved over the top of each other.
I name it “clean” and put it in my personal bin directory. You might add or delete individual line items depending on what kinds of files your programs create. When I was a grad student, disks were expensive, and worked on a system that enforced quotas, I ran it in a cronjob once a day. Now I just run it whenever things look ugly—the same approach I have to dusting.
Building or modifying a script like this can be dangerous since a bug could cause things you care about to be systematically removed. I recommend testing it on an account that doesn’t have anything you care about in it before you blindly trust it.
One last thing: I used Linux in the title, but this will obviously work in anything with bash and find including varieties of Unix and OS X. These days I’m running it on OS X rather than Ultrix or 4.3BSD. Not all versions of find have a “prune” option.
Posted by windley on April 4, 2007 10:41 AM




Comment from Rick Moynihan at April 6, 2007 3:59 AM
Hi Phil,
Nice script. I was planning on writing one of these myself but never got round to it.
I modified it slightly so that it doesn't clean directories called core/ (of which I have a few) but does clean core files . It seems to work in my limited tests.
#!/bin/bash
if [ ! -e $HOME/.rmd ]
then mkdir $HOME/.rmd
fi
find $HOME \( -name '.rmd' -prune \) -o \
\( -name '*~' \
-o -name ',*' \
-o -name '#*#' \
-o -name '*.bak'\
-o -name '*.backup' -atime +5\
-o -name 'core' -type f\
\) \
-print -exec mv -f {} $HOME/.rmd \;
find $HOME/.rmd -atime +5 -exec rm -f {} \;
Comment from Tim Archer at April 8, 2007 3:19 PM
I like your script! Essentially you created a "waste basket" to hold the files for a few days. Good idea!
I did a little writeup on the find command to help me remember the various options I commonly need. Its at:
http://timarcher.com/?q=node/23
Leave a comment
I encourage you to leave a comment below. Your email address will not be displayed on Technometria, but allows me to communicate with you directly. Your email address won't be displayed, but will be used to compute a MicroID for your comment.