Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 ?

share|improve this question
1  
You might be better off if you would give us the details of which OS you're using. –  TheCompWiz Oct 31 '12 at 16:01
    
Just type mv <file> /home/someuser/.trash instead of typing rm <file> –  user21448 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. –  HopelessN00b Oct 31 '12 at 16:22
    
Can you tell me in Linux using inode no. without using alias concept ? –  user143486 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 1 '12 at 10:50
show 1 more comment

migrated from serverfault.com Nov 1 '12 at 10:26

This question came from our site for professional system and network administrators.

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
share|improve this answer
add comment

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 '
share|improve this answer
add comment

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. Hope this helps...

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.