# Instead of using RM. I want to use a custom “soft delete” command

Instead of using the "RM" command for things. I would like to have a custom command that would move the file to a "trash" folder with the dir it had. for example.

If a file was /home/test/folder1/xx/file.so when it was processed with the command, it would move to /trash/home/test/folder1/xx/file.so that way I have no conflicts with something with that name already being in the trash.

Anyone have any ideas how to implement this and also allow it to be used recursively so directories can be trashed too?

-

## migrated from stackoverflow.comJan 19 '10 at 3:54

This question came from our site for professional and enthusiast programmers.

Have a look at trash-cli

-
been using this, and i love it. –  George IV Jan 18 '10 at 5:04
This is what I needed. I prefer to have my own directory and do things my way but this will do what I want. –  Unknown Jan 18 '10 at 5:06
And you can always create an alias to rm, so it will use trash-cli instead of rm. Just put alias rm='trash-put' in your ~/.bashrc and any new shell should use trash when you call rm. If you want to use the real rm just type \rm and it will work. –  Flávio Amieiro Jan 18 '10 at 18:12
I would strongly advise against aliasing rm. You'll be using it happily, safe in the knowledge that you have the trash files in case you get something wrong, and one day you'll find you don't. It might be that you're on a different machine, or that somehow your .bashrc has been overwritten. Much better to have your own command, find it doesn't work and go fix it than permanently delete files by mistake. –  Paul Rayner Jan 19 '10 at 3:18

-

I use:

function trash () { # safe rm to trash
local path
for path in "$@"; do # ignore any arguments if [[ "$path" = -* ]]; then :
else
local dst=${path##*/} # append the time if necessary while [ -e ~/.Trash/"$dst" ]; do
dst="$dst "$(date +%H-%M-%S)
done
mv "$path" ~/.Trash/"$dst"
fi
done
}

Safe, and easy.

-