0

Suppose if I delete any file using the command prompt (I'm talking about Linux), that file is gone, I can't get back that file.

If I by mistake delete any important file, then that would be a big problem for me.

So I want to create one backup folder. If I delete something it should go to that folder first. From the original location it should be deleted, but that file should moved to backup folder.

I am using CentOS 6.2.

1
  • 3
    This is just not "the way". Windows doesn't do this either. If you remove a file from the command line, it will not go into any recycle bin. A recycle bin is a user interface thing, not a system thing. If you want to put things in a recycle bin, then issue a command to put them there. Jun 20, 2012 at 11:31

3 Answers 3

2

Have you tried trash-put from trash-cli package? It sends your files to trash instead of delete them.

3
  • @Devator Aliasing rm is a terribly bad idea. You'll get used to trashing files with it and once you're on another system, where rm works like expected, you suddenly find yourself permanently removing files and not trashing them.
    – slhck
    Jun 20, 2012 at 11:55
  • @slhck Fair enough, the comment has been removed.
    – Devator
    Jun 20, 2012 at 11:56
  • @criziot thanx man your tip is working fine. thanks a lot...
    – max
    Jun 20, 2012 at 12:18
1

Deleted files are not really gone. Tools like ext3grep can usually bring your data back, if you act immediately and know what you're doing.

Generally speaking, though, command line is the user interface for the people who know what they're doing. (Like C/C++ for programming languages.) Any workarounds you might come up with will be fragile. What if you realize your mistake the second you told the system to empty the trash? Will you install a second trashcan so when you empty the first one everything gets send to the second trashcan?

The "way to go" is to not use rm carelessly. The moment you type rm, cut your typing speed in half and think before you hit that "enter" key.

2
  • You're right but my experience tells me that, at least for me, don't matter how hard I try, I will make a mistake and delete important files. So why is it bad send files to trash instead of delete them?
    – fmanco
    Jun 20, 2012 at 12:15
  • 1
    @criziot: The trashcan is an illusion. Files in there are not deleted; they still take up space, and show up in backups. The moment you do delete them (empty the trash), they are gone just as if you'd deleted them with rm. If you want to move files into a trashcan, use mv <file> ~/.trash or somesuch, but don't redefine "delete" to mean something different, because sooner or later, that very redefinition will come back to bite you, either because it's there when you really want to delete something, or because it isn't when you came to rely on it. Say what you mean, mean what you say.
    – DevSolar
    Jun 20, 2012 at 12:48
0

This article has all the information you want. Among other suggestions, it recommends creating an ~/.rm directory where files removed with rm will be placed. Then, you can clear that directory each time you log in:

  1. Add this to your ~/.bashrc file:

    alias waste='/bin/rm'
    alias rm='mv $1 ~/.rm'
    
  2. Add this to your ~/.profile:

    if [ -x ~/.rm ];
     then
       /bin/rm -r ~/.rm
       mkdir ~/.rm
       chmod og-r ~/.rm
     else
       mkdir ~/.rm
       chmod og-r ~/.rm
     fi
    

If you don't want them to be cleared on each login but on each reboot, don't add the lines from step 2 to your profile and, instead, run crotab -e and add this line to the file that will appear:

@reboot [ -x ~/.rm ] && /bin/rm -r ~/.rm; mkdir ~/rm && chmod og-r ~/.rm 
1
  • It was in the review queue. Given 60% of those 25 posts were users with 20k+ reputation I just manually added those comments. I will now deleted the comment and return to our normally scheduled program
    – Ramhound
    Oct 19, 2015 at 17:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .