-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).

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 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
  • But -r asks to the user for each file to delete or not if you don't use -f too. Just ask is not a sufficient safeguard? – Yuri R C Oct 17 '16 at 19:37
  • 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
  • On my Debian system there is no alias to rm . And I'm pretty sure that other distributions/Unix-like systems are the same. – schaiba Oct 17 '16 at 20:01
  • 2
    In a word: safety. – Wildcard Oct 17 '16 at 20:19
  • @YuriRC @schaiba rm is often aliased to rm -i for the root user in most distros. -i is the interactive option, which causes the user to be prompted before removing a file. The standard behavior of rm is to remove the file without asking if the user has permission. The root user has permissions to remove files that can seriously damage the OS, hence the added prompt. – Centimane Oct 17 '16 at 20:49
  • 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

3 Answers3

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.

0

A file is not the same as a directory, nor is it the same as a block or character device file, pipe, semaphore, etc. ...

rm removes a file (normal, device, pipes included), however

rmdir removes a directory.

rm - by default does not remove a directory.

rmdir - by default will not removed a non-empty directory.

rm -d - makes rm accept a directory as well as a file, but it will only remove an empty directory (just like rmdir).

rm -r - will recurse to the bottom of the tree and remove both files and directories, as it move back up the tree. It removes files first, so the directory will be empty, when it attempts to delete it.

rmdir -p a/b/c removes a directory and it's parents, but it will only remove a directory if it is empty. Given a directory structure a/b/c then rmdir -p a/b/c and rm -r a are nearly inverse commands, except rm -r will remove a non empty directory, rmdir will complain.

This gives us quite a flexible system to work with.

There's nothing preventing you create your own alias,

`alias ZapIt='rm -r $*'

mkdir -p a/b/c ; touch a/b/c/fc1
ZapIt a

would delete a/b/c/fc1 a/b/c a/b and a.

X Tian
  • 10,463