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

Make 'rm' behave like the Finder trash UNIX
Out of the many cool hacks on O'Reilly's hacks site is one by Rael Dornfest which replaces the rm command with a new version with Finder-like behavior. The result: if you rm a file in the Terminal, it shows up in the Trash can, waiting for that final confirmation before you manually delete it for good.

[robg adds: Another variation on this script was detailed in this hint.]
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)

Make 'rm' behave like the Finder trash | 11 comments | Create New Account
Click here to return to the 'Make 'rm' behave like the Finder trash' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Why not MvMac?
Authored by: googoo on Apr 22, '03 10:44:55AM

I downloaded the rmm script, and it works very well. One question, though: why does it use CpMac to copy the file to ~/.Trash and then rm to remove it? Could it save a step by just using MvMac to move the file (resource fork and all) to ~/.Trash? Am I missing something?


[ Reply to This | # ]
Why not MvMac?
Authored by: rselph on Apr 22, '03 11:33:04AM

Because MvMac apparently doesn't work accross different file
systems, but CpMac does.

[ Reply to This | # ]
Why not MvMac?
Authored by: jlllopis on Jun 05, '03 07:46:02AM

And why not using MvMac when both source and destination volumes are the same and CpMac otherwhise? This way the speed will be improved in many cases.

[ Reply to This | # ]
Make 'rm' behave like the Finder trash
Authored by: rselph on Apr 22, '03 11:27:24AM

For people who are used to the unix rm command, this script
looks much better than the one in the previous hint. It supports
all of the right command line options, and seems to be very
careful to handle errors correctly.

[ Reply to This | # ]
CpMac a bad choice
Authored by: gatorparrots on Apr 22, '03 01:26:13PM
I don't know why Gary Kerbaugh decided to use CpMac in his rmm script (the script in question). CpMac is only included in the Developer Tools, a 300+MB download and optional install, so it will not be on every Mac.
% which CpMac 
Additionally, CpMac does not preserve permissions:
Utility 	Preserves Permissions? 	Preserves Resource Forks & Creator/Type?
cp 		yes 			no
CpMac 		no 			yes
ditto 		yes 			yes
rsync 		yes 			no
(Chart from:

This may or may not be a problem, especially since the items are destined for deletion anyway. My pedantic nature, however, dictates preserving the file intact until it is actually removed from the filesystem. Therefore, I suggest replacing CpMac in Gary's script with ditto (thus recreating the file exactly, with both permissions and resource fork information intact). Additionally, ditto is part of the default OS X distribution, so it can be counted upon to be on any system upon which this script is deployed.

% which ditto

[ Reply to This | # ]
CpMac a bad choice
Authored by: ashill on Apr 22, '03 04:19:37PM
Are you sure that ditto is not installed by the developer tools? I don't know, but being in /usr/bin doesn't necessarily mean it's included in a default install. For example, gcc was installed by the Developer tools (I'm almost certain):

% which gcc

[ Reply to This | # ]

CpMac a bad choice
Authored by: coyote on Apr 22, '03 04:47:15PM
Additionally, CpMac does not preserve permissions

It does if you use the '-p' flag:
CpMac -r source target

[ Reply to This | # ]

-p flag
Authored by: gatorparrots on Apr 22, '03 07:49:08PM
The -p flag on CpMac does not mean the same thing as the -p flag on cp. Note the fine difference (from man CpMac):
CpMac - copy files preserving metadata and forks

     -p    Causes CpMac to preserve in the copy as many of the modification
           time, access time, file flags, file mode, user ID, and group ID as
           allowed by permissions.

[ Reply to This | # ]
Make 'rm' behave like the Finder trash
Authored by: krishna on May 13, '03 04:59:03PM

Reading this post got me thinking about the proper 'mac' way to do this, so I made an ugly ugly ugly tcsh alias that does something similar.

I prefer mine because it uses the macos's own trash can functions to trash something, including:

* the update of open finder windows
* the little 'ssh' sound
* being able to trash a whole directory at once (and pretty quickly)
* automatic renaming of files in the trash to not overwrite others

rather than relying on the underlying UNIX implementation, (location of trash directory, any other notifications that happen when a file is removed, etc) which conceivably might change with new os revisions.

Note that it makes an assumption about the current finder device being the same as where you are in the terminal (which is fine for me, because I only have one hard drive on my tibook). Note that this should all be one one single line (replace each newline below with a space).

alias del 'perl -MCwd -e ' "'"
'for (@ARGV) { m{^/} or $_ = cwd."/".$_; s{/}{:}g;}
system ("osascript", "-e", "tell application \"finder\"", map({("-e", "delete \"$_\"")} @ARGV),
"-e", "end tell") ' "'" '\!*'

Then try it out:

touch foo
mkdir somedir
del somedir foo

* this really should be a short perl script, rather than an alias, for readability
* this doesn't handle '.' or '..' in the paths
* I don't know of a proper applescript or other function to transform a relative or absolute unix pathname to a mac pathname (which is what applescript seems to require). That would handle the case of the file being anywhere in the unix filesystem.

[ Reply to This | # ]
Make 'rm' behave like the Finder trash
Authored by: krishna on May 13, '03 05:46:49PM

Re: converting unix pathnames to regular ones, I think something like

set thefile to get POSIX file "/Users/krishna/foo.txt"
set thefilename to path to thefile
tell application "finder"
delete thefilename
end tell

should do the trick, but I get all kinds of different weird errors (being just an applescript beginner).

[ Reply to This | # ]
Using MvMac when possible to increase speed
Authored by: jlllopis on Jun 05, '03 08:38:34AM
This script is really great except for one reason: it always uses CpMac even when what we want to remove is in the same volume in which ~/.Trash is. In that circunstance, MvMac will be much faster.

I propose an easy solution: just try MvMac and if it fails (because the volumes are different) try CpMac + rm.

You have to replace de "move_file()" function in the script with this code:

move_file ()
    if [ -n "$verbopt" ]; then
        echo "Moving $1 to trash."
        chflags -R nouchg "$1"
        /Developer/Tools/MvMac "$1" "$dest" || (/Developer/Tools/CpMac "$1" "$dest" && /bin/rm "$1")
    elif [ -n "$forceopt" ]; then
        chflags -R nouchg "$1" > /dev/null 2>&1
        /Developer/Tools/MvMac "$1" "$dest" > /dev/null 2>&1 || (/Developer/Tools/CpMac "$1" "$dest" > /dev/null 2>&1 &&  /bin/rm "$1" > /dev/null 2>&1)
        chflags -R nouchg "$1"
        /Developer/Tools/MvMac "$1" "$dest" || (/Developer/Tools/CpMac "$1" "$dest" && /bin/rm "$1")
(The lines starting with /Developer... are quite long but must be written in one line) Luis

[ Reply to This | # ]