Return to Snippet

Revision: 50048
at August 5, 2011 02:31 by mkornatz


Initial Code
#!/bin/bash
#
#  This script is to be called by an alias of the rm command
#  e.g. using the entry...
#  alias rm='sh /home/user1/my_scripts/rm_replacement.sh'
#  in your $HOME/.profile or $HOME/.bashrc file
#
#  It will move rm files to /home/user1/.RecycleBin
#  You need to create the directory /home/user1/.RecycleBin if
#  it doesn't already exist
#
#  If a file/directory with the same name already exists in the
#  RecycleBin, the file/directory already in the RecycleBin
#  will be renamed taging "_X" on to the end where
#  X is an incremental number and then the newly rm'd
#  file/directory will be copied into the RecycleBin
#
#  Consequently the latest version of the file will be its normall
#  name, without any extension.

RM_OPT=""
FILES=""
VERBOSE="n"
FORCE="n"
VOLUME="/home/user1"

while [ "$1" ]; do
    case "$1" in
        "-v") VERBOSE="y" ;;
        "-f") FORCE="y" ;;
        "--help" | "-h")
            echo "You are running a replacement for rm called by an alias"
            echo "Usage rm [options] file1 file2 ..."
            echo
            echo "Moves the files to /volume1/RecycleBin. Files can be either files or"
            echo "directories."
            echo
            echo "Options:"
            echo " -v           verbose mode"
            echo " -h, --help   this help message"
            echo " --help-rm    help message of /bin/rm"
            echo " -f           force processing with the normal rm command (/bin/rm)"
            echo " All other options are ignored when moving files. When removing"
            echo " permanently, these options are passed to /bin/rm"
            echo
            exit 0
        ;;
        "--help-rm")
            /bin/rm --help
            exit 0
        ;;
        -*) RM_OPT=$RM_OPT" "$1 ;;
        *)
            if [ "$VERBOSE" = "y" ]; then
                echo $1
            fi
            if [ "$FORCE" = "y" ]; then
                /bin/rm -f $RM_OPT $1
            else
                if [ -e "$1" ]; then
                    if [ -e "$VOLUME/.RecycleBin/$(basename $1)" ]; then
                        version=2
                        while [ -e "$VOLUME/.RecycleBin/$(basename $1)_$version" ]; do
                        let version=$version+1
                        done
                        mv "$VOLUME/.RecycleBin/$(basename $1)" "$VOLUME/.RecycleBin/$(basename $1)_$version"
                    fi
                    mv "$1" "$VOLUME/.RecycleBin"
                fi
            fi
        ;;
    esac
    shift
done

Initial URL
http://forum.synology.com/wiki/index.php/How_to_create_a_Recycle_Bin_(or_Trash_can)_for_the_CLI_rm_command

Initial Description
Modified a little bit to make it easier to change the location of the recycle bin.

Initial Title
File & Directory Remove Script

Initial Tags
file, directory

Initial Language
Bash