Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a way to intercept calls to rm, and instead of permamently deleting the files, moving them to trash instead?

share|improve this question
    
How universal are you asking? Do you mean for every rm call by every user or process? Or just for you while you're using bash? Or.. –  djeikyb Nov 1 '11 at 6:47

2 Answers 2

up vote 10 down vote accepted

There's a recipe on webupd8.org for this. To prevent link rot, here's the important information (with a few additions).

sudo apt-get install trash-cli

This will install trash, empty-trash, list-trash and restore-trash commands, which you can use as-is or make rm an alias of trash (see below).

The semantic of the trash command is a bit different then the standard rm - it doesn't require -r flag in order to be able to delete directories. If this bothers you, webupd8.org proposes the following script, which you can put in your PATH and call it trash-rm:

#!/bin/bash
# command name: trash-rm
shopt -s extglob
recursive=1
declare -a cmd
((i = 0))
for f in "$@"; do
    case "$f" in

        (-*([fiIv])r*([fiIv])|-*([fiIv])R*([fiIv]))
            tmp="${f//[rR]/}"
            if [ -n "$tmp" ]; then
                #echo "\$tmp == $tmp"
                cmd[$i]="$tmp"
                ((i++))
            fi
            recursive=0
        ;;

        (--recursive) recursive=0
        ;;

        (*)
            if [ $recursive != 0   -a  -d "$f" ]; then
                echo "skipping directory: $f"
                continue
            else
                cmd[$i]="$f"
                ((i++))
            fi
        ;;

    esac
done
trash "${cmd[@]}"

In Ubuntu 12.04 and later, the last command in the script should be trash-put "${cmd[@]}" instead of trash "${cmd[@]}" (as the command has changed from trash to trash-put).

Then make the script executable:

chmod +x trash-rm

Once you have it in some directory in your PATH, add an alias to your ~/.bashrc, which will make bash to invoke your script instead of the actual rm command:

alias rm="trash-rm"

As djeikyb correctly points out, the .bashrc alias trick would only work for the user whose .bashrc is modified, and only in bash terminal session.

And that should be it.

share|improve this answer
    
This is a good answer, but it would be even better if you reorganised it to emphasise the main concept of using an alias. I think it's most important to understand how to "intercept" rm. What happens in its place can be customised any number of ways. –  djeikyb Nov 1 '11 at 6:44
    
Also, it'd be good to mention that this only affects the rm command for the user's bash session. –  djeikyb Nov 1 '11 at 6:46
    
@djeikyb: thanks for the suggesions, I updated the answer –  Sergey Nov 1 '11 at 7:17
    
It works great for me without the script (just installed and add alias="trash"). Do you know what is the advantage of having the script? –  desgua Jan 29 '12 at 0:03
    
@desgua: the purpose of the script is to make trash to behave more like rm in regards to deleting directories. This may be important for some scripts which expect rm to work the way it does, for example. I updated the answer. –  Sergey Jan 29 '12 at 0:41

libtrash (package libtrash in Ubuntu) seems to do this, but I've never used it.

share|improve this answer

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.