6

I see a lot of resources on rm -r but almost nothing on rm -d even though the d flag is apparently all about deleting directories.

What is the difference between the two exactly? Is rm -d the same as rmdir? Is it "safer" than rm -r?

3
  • 5
    According to the man page, -d will only remove empty directories. -r doesn't care. Sep 28, 2017 at 22:33
  • 1
    Afaik -r only doesn't care if also -f is set
    – derHugo
    Sep 28, 2017 at 22:40
  • 1
    @derHugo -r is recursive, it repeatedly deleted the contents. -f just means don't prompt the user
    – Zanna
    Sep 28, 2017 at 22:48

2 Answers 2

9

I tried both commands and here are my conclusions:

  • rm -d works just like rmdir (unless there's something going on with links or something like that).
  • rm -r deletes the target directory and recursively deletes everything inside that directory (including non-empty directories and files of any type)

So the difference is pretty clear. There's no doubt that rm -r is a really dangerous command that should be used very carefuly (Example: rm -rf / can potentialy delete your system; or rm -rf * could delete your current project), while rm -d will only let you delete empty directories

Warning

Remember that rm -rd is a valid command and it will work like rm -r, so be careful.

7
  • 1
    You're not wrong about rm -rf / being dangerous, however there are protections that help to prevent you from deleting your file system, and you would have to be actively malicious to provide the proper arguments necessary to erase the root system directory.
    – Thomas Ward
    Sep 28, 2017 at 22:44
  • 3
    I know that you have to provide the --no-preserve-root option to do that kind of thing, but the point is pretty clear. Sep 28, 2017 at 22:46
  • rm -d doesn't remove an empty directory on my system with coreutils 6.9 (not Ubuntu), while rmdir does. Not sure why.
    – Ruslan
    Sep 29, 2017 at 7:23
  • @FranciscoGallegoSalido iirc, POSIX states that you shouldn’t be able to remove the current working directory, so it could be argued that rm -rf / shouldn’t work because it would remove the directory you are currently in. Sun Microsystems took this approach in Solaris, so Solaris derivatives implement this afaik. Then again, Linux isn’t Unix and cares less and less about POSIX I feel.
    – forquare
    Sep 29, 2017 at 8:51
  • @forquare Ok, let's say rm -rf * is dangerous, better example? I'm going to edit it. Sep 29, 2017 at 15:07
1

A difference between rm -rd and rm -rf is that the former will prompt you before deleting a write-protected file or directory, while the latter will go ahead and delete write-protected files without prompting if it can do so. While rm -rd and rm -rf are both recursive and potentially dangerous if directed to the wrong directory, the former provides a little more protection against doing something you really didn't want to do.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .