Questions
- What is the difference between the
rm -dandrm -Rcommands in Bash? - Which one should I use?
Details
According to the man page for the rm command:
rm -dattempts to remove directories as well as other types of
files.rm -Rattempts 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.
Ordinarily,
rmwill not remove a directory, even if it is empty.rm -djust makesrmact likermdir. It still refuses to remove the directory if it isn’t empty, but will do so if it is empty.rm -Ris 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 usermdir. I’d usermdir/rm -dif you only want to remove the directory if it is, in fact, empty. Saverm -Rfor when you are fully aware that you are trying to remove a directory and all its contents.