JTechDev

Move to trash bash script

Using the rm -rf [file] command is very useful, but many times it is dangerous. Sometimes it would be nice to have a simple command that moves files to the trash. I created a Bash script that does just that.

Normally, if you would like to move a file to the trash, you would have to type the command: mv [file] ~/.Trash. Not to mention, it is difficult to delete multiple files. Here is the the script I use to delete files to the trash easily:

#!/bin/bash
args=("$@")
MIN_ARGS=1
if [ $# -ge $MIN_ARGS ]; then
    for FILE in "$@"
    do
        mv -v $FILE ~/.Trash/
    done
else
    echo "trash - move items to trash"
    echo " "
    echo "usage: trash [file1] [file2] [etc...]"
fi

I put this in a file called trash and used the command chmod 755 trash to make it executable. I then added it to my $PATH. I now can use the command trash [files] anywhere. I have only tested this on Mac OS X, but it should work on any Unix based system.