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

Questions

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

More details

According to the rm command man page:

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

Now I know the last statement ( -R means -d ), which may seem to be the answer to my question. However, I still wonder why both team flags exist in the first place, if they are supposedly identical in what they do.

In addition, since I am still involved in the Bash training process, I think it’s good to know which option is the preferred choice among Bash programmers (conditionally) and why.

+6
source share
4 answers

Normally rm does not delete the directory, even if it is empty. rm -d just makes rm act like rmdir . He still refuses to delete the directory if it is not empty, but will do so if it is empty.

rm -R is a complete recursive deletion, deletion of the directory and all its contents.

I have never used -d since I did not know that it exists and always just use rmdir . I would use rmdir / rm -d if you want to delete the directory if it is essentially empty. Save rm -R when you are fully aware that you are trying to delete the directory and all its contents.

+9
source

The -d option is especially important for implementing BSD rm , which you will most likely find on your Mac. It is missing from the GNU implementation that you will find on Linux systems.

If you are looking for a preferred option, you will need to use -r (lowercase) to remove entire trees and rmdir to delete individual directories, which you will find more portable code.

+3
source

rm will delete the files, but I had problems deleting the directory, so I need to mark it with some option,

rm -f will force delete the file without asking for confirmation

rm -R will delete the directory, but in my case it asked for confirmation, but since I have so many files, I continued to type y and y, which were taken forever

rm -Rf was my final decision, it forcedly deleted the directory without asking for confirmation

0
source

To be clear, you should never use rm -d . Assuming this is not just a failure with the error message "Operation not allowed", he will delete the directory without deleting the contents. In an empty directory, this is the same as rmdir . In a non-empty directory, it creates inconsistencies in a file system that needs to be repaired with fsck or with a very smart manual hacker.

This is a dumb version that would never exist. When they added, the BSD people had some bad drugs. rm -r been on UNIX since at least 1973, and rmdir since 1971.

-1
source

Source: https://habr.com/ru/post/922986/


All Articles