1

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 ?

manatwork
  • 31,277
  • 1
    You might be better off if you would give us the details of which OS you're using. –  Oct 31 '12 at 16:01
  • Just type mv /home/someuser/.trash instead of typing rm –  Oct 31 '12 at 16:09
  • 4
    Please read the site FAQ. We're not here to do your homework for you... but I have something caustic to say to your teacher about the usefulness of this assignment... let me know if that would be helpful. –  Oct 31 '12 at 16:22
  • Can you tell me in Linux using inode no. without using alias concept ? –  Oct 31 '12 at 16:25
  • It's a terrible terrible thing to alias rm with mv. Without using aliasing, I would do it by taking the source for rm from CoreUtils and modifying it into a new package that implements the required functionality. – darnir Nov 01 '12 at 10:50
  • Why is aliasing rm 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 binary rm 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

4 Answers4

4

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
Mircea Vutcovici
  • 1,834
  • 14
  • 9
1

A quick solution is to add the following function declaration to your ~.bashrc.

function rm() {
   mv "${@}" ~/.trash
}

Caveats:

  • Differences in the options between rm and mv 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 '
1

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.

0

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.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
rahuL
  • 325