Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
7  
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 6: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). –  disappearedng Jul 25 '11 at 9:03
1  
Well you should have mentioned in your question that you have multiple partitions! –  slhck Jul 25 '11 at 11:41
    
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 5 '12 at 5:15
add comment

2 Answers

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
}
share|improve this answer
    
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 –  disappearedng Jul 25 '11 at 9: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
add comment

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 * ;)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.