13

Snow Leopard allows users to "Put Back" files that were moved to the Trash from Finder. However, when I do mv <file> ~/.Trash from the command line I don't get the "Put Back" option.

Is there a native command in Snow Leopard that moves a file to the trash while letting me still be able to us "Put Back"?

7 Answers 7

2

When you move a file to the trash OS X updates the .DS_Store file inside ~/.Trash with the "Put back" information. (See this question for more details)

In order to update this you would have to also update the .DS_Store file when you move a file to the Trash in Terminal.

1
  • Thanks. Not what I was hoping for, but it's the truth.
    – matpie
    Mar 5, 2010 at 21:58
9

I recommend installing the Ruby gem osx-trash.

How?

sudo gem install osx-trash

Usage?

Usage: trash [OPTIONS] <file> [<file> ...]
       trash [-e | --empty]
       trash [-l | --list]
Specific options:
    -e, --empty                      Empty the trash
    -l, --list                       List items in the trash
    -h, --help                       Show this message
    -v, --version                    Show version

Manipulate to the Finder's trash.
3
  • 2
    Nice! Though, as it communicates with Finder, trashing files with a wildcard plays the trashing sound for every file. Mar 20, 2011 at 17:59
  • 1
    I don't think this still works. From the docs: osx-trash requires Mac OS X 10.5, Leopard. I tried it and all I get is bash: trash: command not found Aug 23, 2013 at 2:46
  • Doesnt work on OS X 10.9 asked a fresh question about how to fix it here superuser.com/questions/694433/…
    – simbo1905
    Dec 29, 2013 at 9:35
2

In the MacOSX SDK, there are the functions FSPathMoveObjectToTrashSync and related. It should be easy to write a Python (or whatever) script around it.

Edit: Just coded it. Can be found here: rm-trash.py.

1
  • FYI - FSPathMoveObjectToTrashSync is deprecated in 10.8 and according to the doc you will need to use trashItemAtURL:resultingItemURL:error: instead. Jan 10, 2013 at 23:05
2

You want the rmtrash program.

You can use it to add items to the Trash as if it was done in the finder.

Edit: I just tried using rmtrash to delete and recover. It works exactly like deleting from finder. Files show up in the Trash, can be dragged out and emptied exactly the same. Been using it since '06 or so.

1
  • From the code of rmtrash, it looks like it is just equivalent to mv file ~/.Trash/. Are you sure that you can recover the files from this tool?
    – Albert
    Jun 4, 2012 at 19:25
0

After looking at the alternatives I found out that:

  • The rmtrash program moves files to the trash directory without using the system API, so the .DS_Store file won't be updated. Also, doesn't trigger the recycle sound.
  • The python script invokes a deprecated method on mountain lion which also doesn't trigger the recycle sound.
  • The ruby script works, but is sloooooow (like the python one).

So I wrote my own trash command implemented in the Nimrod programming language, so its fast and it triggers the trashcan sound in a background process. This means that typing the command and running it exits immediately as it doesn't need to wait for the sound to play. The con is you need to compile nimrod and then this program (no binary sorry!).

PS: To be honest the best implementation would be that of rmtrash using the official API I'm using from Nimrod, which is plain objective-c.

0

Check out trash-cli. It works cross-platform, no trash sound, and supports Put Back.

You can install it with (requires Node.js):

$ npm install --global trash-cli

Alternatively, if you don't want to use Node.js, you can install the native binary osx-trash manually.

-1

I had the same problem today and checked also the rmtrash program given above. Unfortunately all solutions seen so far move files to the home directory's .Trash.

The Finder on the other hand moves it to different -Trash folders, based on the mount point of the filesystem of the file.

So the easiest solution I can come up with (created on 10.5.8. To be tested on newer versions of OS X) is this:

osascript -e 'tell application "Finder" to delete POSIX file "'FULL FILENAME HERE'"'

Here is the shell script I created for it:

#!/bin/bash
declare -a files
for f in "$@" ; do
    if [ -r "$f" ] ; then
        case "$f" in
        /*) ;;
        *) f="$( pwd )/$f" ;;
        esac
        files=("${files[@]}" "$f")
    else
        echo "Can't find '$f'" >&2
    fi
done
if [ ${#files[@]} -gt 0 ] ; then
    osascript -e '
    on run argv
        repeat with f in argv
            set x to (POSIX file f) as string
            tell application "Finder" to delete x
        end
    end
    ' "${files[@]}" > /dev/null
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .