One thing that always bugged me:

touch a
rm a

ok, it works as expected

mkdir a
rm a

will require -r to work; but I don't understand what is recursive in this? if the folder is empty, there is nothing recursive about it. I would expect rm a to try to remove a, and fail if it's not empty and rm -r to recursively the contents of a.

why is is this like that?

Edit: this question has been flagged as a duplicate of a totally irrelevant question. My question can be summed as: Why do I need a recursive flag to perform a non recursive operation: mkdir a, rm -r a has nothing recursive about it. And this is not related to the other question which is about why rm is not recursive by default.

  • 3
    You might be looking for rmdir. – Ulrich Schwarz Nov 7 at 13:42
  • 1
    Possible duplicate of Why rm does not work recursively by default? – muru Nov 7 at 13:53
  • 2
    @muru: it's not the question. I am asking why we need a recursive flag to do a non recursive operation. – Thomas Nov 7 at 13:55
  • Uh, you have to recurse into the directory to know that it's empty in the first place. Remember that deleting something is an operation on the parent directory, not on the thing itself. The parent directory can't tell you whether the target directory is empty. – muru Nov 7 at 14:04
  • 1
    @Thomas rm is one of the basic commands and they usually also strive for speed. Declining removal for a directory in non-recursive mode is quicker than checking if a directory had entries for every option. Also the general behavior would be IMHO less straight forward -> rm * == removes all files and directories but only if they are empty vs. rm * == removes all files and files only – Fiximan Nov 7 at 14:16

This is because rm was never meant to delete directories at all.

Originally rm didn't support -r and users had only 1 option: To delete all files in a dir with rm and after that to remove (the now empty) dir with rmdir

Obviously working like this is very annoying so -r was implemented to fix this problem.

But if rm /some/empty/dir would start working than it would no longer be backwards compatible (so it could break old code that assumed that rm /some/empty/dir fails). With -r this isn't a problem because this behaviour wasn't defined yet (because the -r option didn't exist yet)


From man rm:

By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents.

I'll take a guess here: It's a safety feature that you do not delete directories with files in there without realizing. I assume rm would not check if a directory had content in first place (in contrast to rmdir).

EDIT: I assume without recursively deleting files, there would be a problem in terms of finding a file as the names and inodes are stored in the directory. Deleting the directory would create "zombie"-files with inodes but no filename. Compare to this post on EXT structures.

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.