caleb531
caleb531

Reputation: 4361

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

Questions

Details

According to the man page for the rm command:

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.

Upvotes: 10

Views: 13695

Answers (4)

Abdur Rahman Turawa
Abdur Rahman Turawa

Reputation: 581

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

Upvotes: 1

Alan Curry
Alan Curry

Reputation: 14711

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.

Upvotes: -1

Grisha Levit
Grisha Levit

Reputation: 8617

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.

Upvotes: 3

chepner
chepner

Reputation: 530833

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.

Upvotes: 12

Related Questions