Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to rm things to the trash UNIX
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!]
    •    
  • Currently 3.20 / 5
  You rated: 5 / 5 (5 votes cast)
 
[10,694 views]  

A script to rm things to the trash | 10 comments | Create New Account
Click here to return to the 'A script to rm things to the trash' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to rm things to the trash
Authored by: mholve on Feb 27, '03 11:54:59AM

You should place the script in /usr/local/bin and perhaps make a symlink in /bin thusly; ln -s /usr/local/bin/rrm /bin/rrm. It seems /usr/local/bin is not in the path by default, YMMV.

---
--
Everything Mac - http://everythingmac.org



[ Reply to This | # ]
A script to rm things to the trash
Authored by: ncianca on Feb 27, '03 12:25:17PM

I use a similar script in /usr/local/bin and I make an alias to rm in my .cshrc file:

alias rm '/usr/local/bin/rrm \!*'

You should also put the alias in the root .cshrc file so that it works in root as well.



[ Reply to This | # ]
A script to rm things to the trash
Authored by: torkel on Feb 27, '03 04:03:56PM

What about the resource forks? Shouldn't you be using MvMac(1)? Or rather CpMac(1) and then rm, since MvMac alledgedly does not work across file systems.



[ Reply to This | # ]
A script to rm things to the trash
Authored by: TigerKR on Feb 27, '03 08:35:25PM

CpMac is not part of the default Mac OS X 10.2.4 install. Where would I download it?



[ Reply to This | # ]
A script to rm things to the trash
Authored by: TigerKR on Feb 27, '03 08:42:02PM

I found it here: /Developer/Tools/CpMac
and I symlinked it to: /usr/local/bin/CpMac

I have the developers tools installed.



[ Reply to This | # ]
A script to rm things to the trash
Authored by: daveron on Jun 29, '09 08:33:04PM

As of Mac OS X 10.4, the mv command preserves metadata and resource forks
of files on Extended HFS volumes, so it can be used in place of MvMac.
The MvMac command will be deprecated in future versions of Mac OS X.



[ Reply to This | # ]
another variation
Authored by: awk on Feb 27, '03 05:14:24PM

What I have in my aliases file:

alias trash 'mv --verbose --backup=numbered \!* ~/.Trash/'

This will use the built-in backup facilities of "mv" which names backups "file", "file.~1~", "file.~2~", etc.. not as nice as "file copy", "file copy 2" but then again it's just the trash. :-)



[ Reply to This | # ]
oops! requires GNU fileutils
Authored by: awk on Feb 27, '03 05:18:22PM

Oops, just realized this only works with the GNU fileutils' /sw/bin/mv that I installed with Fink. Never mind....unless you have it installed already... the fileutils versions have a lot more options.



[ Reply to This | # ]
A script to rm things to the trash
Authored by: Reaper Man on Feb 28, '03 10:08:22PM

I was surprised no one commented that this is not how the Finder renames files to the trash. I recently implemented this in C++ for our own purposes.

The Finder actually renames the item in the trash before moving the file to the trash. Give it a try. Check the dates of files with the same name after sending them to the trash.

The technique in the script works because mv can rename as it moves. There is not the (easy) capability to that using traditional Mac OS file APIs.



[ Reply to This | # ]
A script to rm things to the trash
Authored by: nherring on Apr 30, '07 12:57:32PM

If you have multiple volumes, there are trash folders per volume. You may end up with unexpected failures if you move a file from one volume that has enough space to hold it to your boot volume that doesn't. It's not immediately obvious to me how one would determine what volume the file is on (though you could use some heuristics involving whether the full path started with /Volumes or not) in order to determine which .Trashes folder to use. Furthermore, Apple may very well change their implementation of the trash folder...

Ultimately, it might actually be cheaper to farm the entire process off to the Finder (assuming you're in a LoginWindow session) using an apple event via osascript. That way you get the stock Apple filename collision renaming scheme for the OS, etc.



[ Reply to This | # ]