A script to rm things to the trash

Feb 27, '03 08:48:00AM

Contributed by: semios

Rather than rm-ing things in unix land, why not use the GUI trash facilities? This script will move things to the trash instead of irrevocably deleting them. It tries to emulate the same behavior of the Finder, which appends "copy x" to the name if necessary.


#!/bin/sh
# trash

# @author Shane Celis <shane (at) gnufoo (dot) org>

if [ $# -eq 0 ]; then
  echo "usage: trash <files...>" >&2
  exit 2;
fi

for file in "$@"; do
  # get just file name 
  destfile="`basename $file`"
  suffix='';
  i=0;

  # If that file already exists in the trash, change the name using
  # the same convetion OS X does.

  # blah
  # blah copy
  # blah copy 1
  # etc...
  while [ -e "$HOME/.Trash/${destfile}${suffix}" ]; do
    if [ $i -eq 0 ]; then
      suffix=" copy";
    else
      suffix=" copy $i";
    fi
    i=`expr $i + 1`
  done
  mv "$file" "$HOME/.Trash/${destfile}${suffix}"
done
[robg adds: I created this script in my ~/bin directory, named it 'rrm', made it executable with chmod 755 rrm, and tested it. It seems to work exactly as expected, though I will admit I didn't test it on all possible filenames (it seemed to handle names with spaces just fine, though). If 'rm' and its insta-gone feature scares you a bit, this seems to be a safer way to go!]

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030217172653485