, ,

Ubuntu – rm – Delete to Trash

Method 1 – rm alias to mv to Trash

# in your aliases – e.g. ~/.bash_aliases – add this line

alias rm="mv -t ~/.Trash"

alias rm='mkdir -p ~/.Trash;mv -n -b -t ~/.Trash'

Method 2 – using trash-cli

trash-cli, in Python
trash-cli: (Open Source) Command line interface to the Linux trash can. Interoperates with all the desktop trash cans (KDE/Gnome/Xfce). Included in some Linux distributions. Proudly written in Python.
Written by italian developer Andrea Francia

https://github.com/andreafrancia

http://www.andreafrancia.it/code.html

# installs commands: trash, empty-trash, list-trash and restore-trash
sudo apt-get install trash-cli -y --force-yes

# in your aliases – e.g. ~/.bash_aliases – add this line

alias rm="trash"

trash-put trashes files and directories.
trash-empty empty the trashcan(s).
trash-list list trashed file.
restore-trash restore a trashed file.
trash-rm remove individual files from trash can.

# rm on directories does move them to the trash too
# if you don't like this do the following:
cat trash-rm.sh
------------------------------
#!/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[@]}" # all OS versions before Ubuntu 12.04
trash-put "${cmd[@]}"
------------------------------
chmod +x trash-rm.sh
alias rm="trash-rm.sh"

References

http://askubuntu.com/questions/74269/intercepting-calls-to-rm-and-directing-deleted-files-to-trash-instead
http://www.webupd8.org/2010/02/make-rm-move-files-to-trash-instead-of.html