O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  



Bringing Trash-Awareness to rm
Ever find it frustrating that you chose to rm something rather than moving it to the Finder's Trash and not being able to undo your actions later? Born Again Remove Function to the rescue!

Contributed by:
Rael Dornfest
[04/10/03 | Discuss (14) | Link to this hack]

The Born Again Remove Function overrides your built-in rm with a clever script that operates under the principle of least surprise, mimicking on the command-line what one has come to expect in the Finder.

rm something and it appears in your Trash can along with everything else you've thrown away. rm -r whole directories and you'll find them and their contents in your Trash, safe and sound. Drag mistakenly rm'd files and folders right out of the Trash; the script uses CpMac, so those valuable resource forks aren't lost along the way.

Download The Born Again Remove Function and save it as rm to a bin folder in your Home directory, creating bin if it doesn't already exist. This can easily be done on the command-line like so:

% cd
% mkdir bin
% cd bin
% curl -o rm "http://www.cs.ecu.edu/~kerbaugh/rm.txt"
Next, we'll make the script executable:
% chmod 700 rm
And, finally, alias it to rm so that it overrides the default /bin/rm. Assuming you're using the default tcsh shell on the command-line, you should put the alias into your .tcshrc (tcsh shell resource) file so that it is read each time you start a new command-line session. Do so like this:
% echo "alias rm ~/bin/rm" >> ~/.tcshrc
You can either close your shell session and open a new one to have the Born Again Remove Function take effect or reload your tcsh resources by typing:
% source ~/.tcshrc
You can see all this taking effect in the following snapshot from my command-line session:
% which rm
/bin/rm
% rm
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file
% source ~/.tcshrc 
% which rm
rm:      aliased to /Users/rael/bin/rm
% rm
Usage: rm
rm v0.0.0.5: options (-dfirv)
    required arguments: [filename]
    Move the FILE(s) to the user's Trash.

    -d, --directory
        unlink  directory,  even        if  non-empty (super-user
        only)

    -f, --force
        ignore nonexistent files, never prompt

    -i, --interactive
        prompt before any removal

    -r, -R, --recursive
        remove the contents of directories recursively

    -v, --verbose
        explain what is being done

    --help display this help and exit

    --version
        output version information and exit
In the following interactive session, I take a gander at the contents of my Trash can (aka ~/.Trash), create a file and throw it away, create a file with the same name, and throw that away. You'll notice that the script is smart enough to rename the second trashed file as somefile copy.txt so as not to clobber the first file living in the Trash.
% ls ~/.Trash/
% touch somefile.txt
% rm somefile.txt
% ls ~/.Trash/
somefile.txt
% ls ~/.Trash/
% touch somefile.txt
% rm somefile.txt
% ls ~/.Trash/
somefile copy.txt   somefile.txt


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.