4

Is there any resultant difference on disk between using rm or rmdir to remove a directory?

Or is it only different by usage preference, By example, given

$ mkdir a
$ mkdir a/b
$ mkdir a/b/c

Would

$ rmdir -p a/b/c

have the same effect as

$ rm -r a

just looking at it from a different end?

4
  • 1
    rmdir is a little safer, sometimes. If you issue mkdir test and then touch test\a and then rmdir test, you will get an error that the directory is not empty. If you issue rm -r test..... Dec 9, 2015 at 21:06
  • Thanks Charles. That does appear to be another context usage difference to keep in mind. :) I am still curious to know if rmdir does something extra on disk that rm -r does not. Dec 9, 2015 at 21:15
  • 2
    There is a good hystorical essay about this on SuperUser Dec 9, 2015 at 21:26
  • 1
    rmdir removes only empty directories , while rm -r will remove even non-empty directories Dec 9, 2015 at 22:07

1 Answer 1

8

rmdir will only remove empty directories while rm -r will remove directories and files within them recursively. Thus, it is safer to use rmdir.

It can save one when hidden files get involved since a cp * or mv * won't copy or move the hidden files along with the rest. rmdir will refuse to delete the folder if there is anything in, even hidden files so it serves as a additional protection in cases like these. Of course, you could just do rm -ri if you wanted to be really careful, but personally, I find rmdir much faster.

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.