Tuesday, December 09, 2008

Have your ever typed rm -rf * by mistake?


Have your typed rm -rf * in Unix/Linux by mistake? What did you do after typing it? I used to get suggestions like use alias rm='rm -i' which asks for confirmation after every rm command. But, for those who use rm command very frequently, it is very inconvenient to type y after every rm command. Even if we do that, for advanced users, after some time, even that will not serve the purpose. Because, for advanced users rm -rf * <CR> y is an atomic operation. i.e., After typing y for many rm commands, they would automatically type y for all rm commands. So, using alias rm='rm -i' is of no use.

I realized all these after typing rm -r * by mistake. After realizing, I created an executable file rm with contents mv $* /tmp, and put the file in a directory, which is in the path before /bin. Now, whenever I type rm something, it moves that file/directory to /tmp. The files would be automatically deleted after restarting the machine. Before restarting the machine, if I want to get back the files, I can get it. There are problems like, if I want to delete files with the same name, then it will fail. If the machine is not restarted regularly, then there will be too many files in /tmp. But, these are not big issues for me.

No comments:

Post a Comment