I want to configure rm
command. When one types rm
to delete a file, then instead of deleting it right away, the file has to be transferred to the .trash of home folder. Can someone help me ?

- 31,277
4 Answers
You can use LD_PRELOAD with libtrash.
See: http://www.techrepublic.com/article/safely-delete-linux-files-with-libtrash/5034918
You can also create hard links to your files in ~/.trash folder. When normal files (that were previously hardlinked) gets deleted, they will remain on the disk until you delete them from the "trash".
To create a hard link:
ln test.txt ~/.trash/test.txt

- 1,834
- 14
- 9
A quick solution is to add the following function declaration to your ~.bashrc
.
function rm() {
mv "${@}" ~/.trash
}
Caveats:
- Differences in the options between
rm
andmv
are note covered. - You can be get used to the trash bin behaviour. On a different machine/account without this modification you will come in great troubles after a quick delete ;-)
Just my 5 cents - use instead:
alias rm='rm -i '

- 2,009
Keep in mind, that it's not a good idea to actually rename or move your "rm" command. A much better practice is to alias
the rm
command to use a completely different script under a user's profile.
in your .bash_profile (assuming you're using bash as your default shell)
alias rm='~/bin/deleteFile'
and then you can create the script ~/bin/deleteFile
. Just copy/modify the following to suit your needs:
#!/bin/bash
TODEL=$1
if [ "${TODEL:0:1}" == "/" ]; then #full path?
TRASH=~/trash-can/`dirname $1`
else #relative path.
TRASH=~/trash-can/`pwd`/`dirname $1`
fi
mkdir -p $TRASH 2> /dev/null
mv $TODEL $TRASH
This is untested, but should work for your needs. There are a few caveats that should be addressed... but it's up to you to decide how it should be handled. i.e. what happens when you delete a file, create a new one with the same name and then delete it? and this script will not clean up your trash-can. Perhaps a cron-job once a week or some such?
This will NOT override a script that calls /bin/rm
directly, nor will it work with scripts that are not loaded within your bash profile. There are probably hundreds of additional things that should be considered before trusting this script to any degree.

- 121
On Debian, something like 'alias rm='mv -t ~/home/.trash/ ' in ~./bash_aliases should work. However, it isn't a good practice to alias rm to mv as it might affect the other users who try to delete permanently using rm. Furthermore, if you go down the aliasing route, you might have to set up a cron job to periodically empty trash.
There're a bunch of alternatives given here - mostly 3rd party tools/scripts.

- 56,709
- 26
- 150
- 232

- 325
rm
withmv
. Without using aliasing, I would do it by taking the source forrm
from CoreUtils and modifying it into a new package that implements the required functionality. – darnir Nov 01 '12 at 10:50rm
a bad thing? ... It would only affect applications running within the user's session (and the user itself). It's not system-wide. Modifying the binaryrm
could have a much longer list of side-effects... not to mention if it breaks, you will have a much harder time of recovering. – TheCompWiz Nov 01 '12 at 15:35