Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

terminal

rm

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.

like image 342
caleb531 Avatar asked Aug 15 '12 20:08

caleb531


People also ask

What is the difference between rm and rm R?

rm -r will recursively delete a directory and all its contents (normally rm will not delete directories, while rmdir will only delete empty directories).

What is the difference between rm and RF?

rm command in UNIX stands for remove and by default is used for removing files. It is simple but a powerful command especially when used with options such as -rf which allow it to delete non-empty directories forcefully.

Why would you use the command rm R instead of rmdir to remove a directory with multiple sub directories and files in it?

You should use rm -r when wanting to recursively remove a directory and all its contents, or when the target may be a directory or file and you want to delete it regardless of what it is. You should use rmdir when wanting to remove an empty directory.

Does rm * remove all files?

The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory. User confirmation, read permission, and write permission are not required before a file is removed when you use the rm command.


2 Answers

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.

like image 88
chepner Avatar answered Sep 20 '22 15:09

chepner


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.

like image 34
Grisha Levit Avatar answered Sep 24 '22 15:09

Grisha Levit