3

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?

  • 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..... – Charles Green Dec 9 '15 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. – SpeedCoder5 Dec 9 '15 at 21:15
  • 2
    There is a good hystorical essay about this on SuperUser – Charles Green Dec 9 '15 at 21:26
  • 1
    rmdir removes only empty directories , while rm -r will remove even non-empty directories – Sergiy Kolodyazhnyy Dec 9 '15 at 22:07
7

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.

| improve this answer | |

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.