Bash: What's the difference between "rm -d" and "rm -R"?
Asked Answered
K

4

10

Questions

  • What is the difference between the rm -d and rm -R commands in Bash?
  • Which one should I use?

Details

According to the man page for the rm command:

  • rm -d attempts to remove directories as well as other types of files.
  • rm -R attempts to remove the file hierarchy rooted in each file argument. The -R option implies the -d option.

Now, I am aware of that last statement (-R implies -d), which may seem to answer my question. However, I still wonder why both command flags exist in the first place, if they are supposedly identical in what they do.

Furthermore, because I am still in the process of learning Bash, I think it's good to know which option is the preferred choice among Bash programmers (conventionally), and why.

Kruter answered 15/8, 2012 at 20:34 Comment(4)
What weird rm is is that you people are discussing? My bog standard GNU coreutils rm doesn't have a -d option.Crites
The BSD version of rm has -d. Whereas the POSIX standard says that rm should behave identically to rmdir when the target is a directory, the BSD version follows the historical behavior of rm unless you use -d.Spathe
The -d option isn't in POSIX, so this question should be tagged for the specific system where the question is directed.Constitutionality
* rm -R attempts to remove the file hierarchy rooted in each file argument. The -R option implies the -d option. <-Can anybody interpret the word "file argument" in here? and furthermore I don't understand the entire sentence completely.-> "remove the file hierarchy rooted in each file argument".Bigname
S
12

Ordinarily, rm will not remove a directory, even if it is empty. rm -d just makes rm act like rmdir. It still refuses to remove the directory if it isn't empty, but will do so if it is empty.

rm -R is the full recursive delete, removing the directory and all its contents.

I've never used -d, as I didn't know it existed and always just use rmdir. I'd use rmdir/rm -d if you only want to remove the directory if it is, in fact, empty. Save rm -R for when you are fully aware that you are trying to remove a directory and all its contents.

Spathe answered 15/8, 2012 at 20:37 Comment(0)
M
3

The -d option is particular to the BSD implementation of rm, which is the one you are likely finding on your Mac. It is not present in the GNU implementation you will find on Linux systems.

If you are looking for the preferred choice, it would be to use -r (lowercase) to remove whole trees and rmdir for removing single directories, which you will find to be code that is more portable.

Manvell answered 15/8, 2012 at 20:45 Comment(0)
H
1

rm will delete files, but i had issues trying to remove directory, so it need to be flagged with some option,

rm -f will force remove file without asking for confirmation

rm -R will remove directory but in my case was asking for confirmation, but because i have so many files, i kept on typing y and y which was taken for eternity

rm -Rf was my final solution, it forced remove the directory without asking for confirmation

Hippocampus answered 6/8, 2016 at 10:29 Comment(0)
I
-1

Just to be clear, you should never use rm -d. Assuming it doesn't just fail with an "Operation not permitted" error message, it will remove the directory without removing the contents. On an empty directory it's the same as rmdir. On a non-empty directory it creates an inconsistency in the filesystem requiring repair by fsck or by some very clever manual hacking.

It's a stupid option that should never have existed. The BSD people were on some bad drugs when they added it. rm -r had been in UNIX since at least 1973, and rmdir since 1971.

Iridescent answered 15/8, 2012 at 21:5 Comment(1)
rm -d fails with "Directory not empty" for me if the directory is not empty. Perhaps the behavior changed at some point.Spathe

© 2022 - 2024 — McMap. All rights reserved.