10

I am used to having rm to alias something safer.

I have been using osx-trash, but once I upgrade to Ruby 1.9.2, this stops working.

I know about trash-cli from Python, but I would much prefer something that moves files to the Trash folder in OS X rather than another directory.

Does anyone have a good solution?

slhck
  • 223,558
  • 70
  • 607
  • 592
xjq233p_1
  • 801
  • 1
  • 11
  • 19
  • 9
    Please, don´t get used to an aliased `rm`, rather get used to `mv files/to/be/deleted ~/.Trash`. It´s safe *and* consistent across systems. – Asmus Jul 25 '11 at 06:45
  • Asmus: You see that only works if your mac has only 1 partition. If you have another partition, moving to ~/.Trash is not really correct (my ~ partition is not big enough). – xjq233p_1 Jul 25 '11 at 09:03
  • 1
    Well you should have mentioned in your question that you have multiple partitions! – slhck Jul 25 '11 at 11:41
  • 1
    disappearedng: well, I think `mv` is still your safest bet. You could easily create a new folder on your other partition called `Trash` (or however you like it) and then `mv` the files there. Once you´re sure they can be deleted, delete and recreate that complete folder. – Asmus Jul 26 '11 at 14:52
  • and then you can just have ~/.Trash be a soft link to the Trash folder on the partition that has enough space – AlcubierreDrive Jan 05 '12 at 05:15

3 Answers3

9

For OSX, you can use safe-rm

brew install safe-rm

You need to do an extra step to prefer it over default rm - add a symlink.

echo $PATH

For me, I have /usr/local/bin: first, the same dir as safe-rm install

Add symlink to safe-rm

ln -s /usr/local/bin/safe-rm /usr/local/bin/rm

To remove and restore the default rm you can remove the symlink rm /usr/local/bin/rm

The config file can be added at /etc/safe-rm.conf

e.g:

/private
/Applications
/Developer
/Library
/Network
/System
/Users
/Volumes
/test

Test with rm -rf /test it will deny delete from safe-rm.conf

David Thomas
  • 191
  • 1
  • 4
4

hasseg.org/trash is an Objective-C utility like osx-trash.

I've written a shell function that doesn't overwrite files like mv * ~/.Trash would. It always moves items to the startup volume though.

trash() {
    for f in "$@"; do
        bn=$(basename "$f")
        while [ -e ~/.Trash/"$bn" ]; do
            bn="$bn $(date +%H.%M.%S %p)"
        done
        mv "$f" ~/.Trash/"$bn"
    done
}
Lri
  • 40,894
  • 7
  • 119
  • 157
  • Yeah I have more than 1 partition and I would want to keep the deleted files in the same partition rather than moving them across – xjq233p_1 Jul 25 '11 at 09:03
  • I added links to two other utilities and another function. Each of them should keep the files on the same partition. – Lri Jul 25 '11 at 22:56
3

Introduction (you can skip it)

For me rm -i would be enough, because it prompts you before deleting any file. However, some extra security provided by safe-rm isn't a bad idea at all.

A simple alias like rm=safe-rm -i could be enough, however rm -rf would not work, because -i is overriden by -f.

So, we have to make sure that -i always come after -f!


My solution (3 steps):

  • Install safe-rm: brew install safe-rm

    You can still work with rm, but you have modify the RM_BIN in the next step

  • Edit your ~/.profile (or bash_profile or whatever), and put the following code snippet, (which contain an alias and a function):

    alias rm="rm_i"
    
    function rm_i(){
    RM_BIN=safe-rm # you can replace it with regular rm if you like
    
    args=""
    files=""
    argsDone=0 # to make sure arguments are before the files
    
    for var in "$@"
    do
        if [[ $var == \-* ]] ; then
            if [ $argsDone -eq 1 ] ; then
                $RM_BIN # just to show the usage of rm
                return
            fi
            args+=" $var"
        else
            argsDone=1
            files+=" $var"
        fi
    done
    
    args+=" -i" # Put -i at the end (so rm -rf will not ignore it)
    
    $RM_BIN $args $files
    }
    
  • Finally, logout and login, so the updates in the .profile are applied. DON'T use it before a logout/login!


Notes:

  • It can be applied to any Linux distro, with slight modifications.
  • It does NOT move your staff to thrash. It just asks you before deleting each file!
  • It works with -rf and with regexes.
  • If you really want to use rm -rf WITHOUT being asked for every single file/folder you can still use /bin/rm -rf (or safe-rm -rf if you had installed it).
  • No more unintentional rm -rf or rm * ;)
Paschalis
  • 131
  • 7