-2

I was discussing with a friend trying to figure out why rm command does not, for example, check if the path is to a file or a folder automatically and delete it recursively, by default, if necessary. Without the needs of the "-r" all the time for a pretty common task. I know that I could put an alias for that. But what I really want is to know the reason (I think that exists a good one, I just don't know).

  • The -r flag to rm already existed in V4 (1973), so this will involve some serious digging! – Stephen Kitt Oct 17 '16 at 19:23
  • 7
    It's not really hard to understand the reason, -r is a safeguard to avoid unintended remove directory by typo which normally give more harm than you lose a single file. – 林果皞 Oct 17 '16 at 19:26
  • 5
    No, it will not ask if you are not using -i. Check your alias of rm by typing type -a rm. – 林果皞 Oct 17 '16 at 19:41
  • 1
    In a word: safety. – Wildcard Oct 17 '16 at 20:19
  • 2
    I don't think that the downvoting on this question is appropriate, the question can be boiled down to: why did the authors of rm choose for it not to be recursive by default?. We've pointed out that it acts as a safeguard, and perhaps that, combined with convention (most commands don't act recursively unless told to, with some exceptions like find) is why the authors chose this direction, but the question would still have a definitive answer. – Centimane Oct 17 '16 at 20:56
1

For the actual reason you should ask the author(s). I can only tell you this is a very good practice:

From Wikipedia article on railway air brake:

The Westinghouse system uses air pressure to charge air reservoirs (tanks) on each car. Full air pressure signals each car to release the brakes. A reduction or loss of air pressure signals each car to apply its brakes, using the compressed air in its reservoirs.

It means the default state is the brake being applied. You need an effort to release it and put the train in a potentially dangerous state.

Of course it doesn't explain why there are the two rm modes (recursive and non-recursive) in the first place. Maybe this is what you ask. However, as they are, making the safer one the default was The Right Thing to do.

0

A second hypothesis would be that the recursive mode was an afterthought.

After all plain rm isn't safe either. And it's not necessarily even safer, as different files are not equally important or irreplacable. What would you rather delete: a directory cloned from Github with the -r flag or /boot/vmlinuz* without the -r option.

With rm you shoot yourself in the foot. But with rm -r you shoot every foot you see, if you're lucky none of those feet are one of your own.

And rm -r does something completely different than plain rm, it has to unlink(2) every single file walking down the tree and then rmdir(2) the empty directories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.