-1

I would like to know when to use rmdir and when do we use rm -rf while removing a directory.

Also is it compulsory to mention the directory path while removing it?

David Foerster
  • 36,264
  • 56
  • 94
  • 147
lad
  • 35
  • It's probably never a good idea to use the -f flag. Most people that use it try to look cool, rather then know why. Of cause, they just make fools of themselves without ever noticing. rm -r and rmdir work just as well. ...not sure what you mean about 'compulsory to mention'. Might want to expand on that. – mikewhatever May 12 '16 at 17:25
  • 1
    "is it compulsory to mention the directory path while removing it" Do you think the computer can read your mind to find out which directory you want to remove? –  May 12 '16 at 17:57
  • 1
    @BharadwajRaju Perhaps they meant the full absolute path of the directory, as opposed to the relative path, and worded the question poorly. When in doubt, it's good to assume the more charitable explanation. –  May 13 '16 at 00:29
  • Strongly related (but imho no duplicate): https://askubuntu.com/questions/217893/how-to-delete-a-non-empty-directory-in-terminal – David Foerster Dec 10 '17 at 23:42

2 Answers2

6
  • rmdir can only remove empty directories
  • rm -r removes a folder recursively (all of its content, then the folder itself)

My advice is to use rmdir everytime you want to remove a directory that should be empty. If it isn't empty rmdir will fail. It is a good practice that will prevent unwanted deletion, hidden files for example.

  • So that means we cannot use the option -r with rmdir.And to delete a directory recursively we need to use rm -ir(as mentioned byDavid in the next post (i to be safe that we are not screwing up))? – lad May 14 '16 at 18:44
  • Exactly, rmdir doesn't have a recursive (-r) option. Used with rm, the -i option will ask for your confirmation before each removal, most of the time you don't need that. Don't use -i every time you remove a folder, use it wisely like the -f option. If you need more informations about those commands and their options, use the "man" command: man rm, man rmdir. With most commands you can also use the "--help" option: rm --help, rmdir --help. – Ronan Boiteau May 14 '16 at 18:59
2

Although Ronan did give a pretty good answer, there is also a more thorough difference, that can be seen by inferring what the command stands for.

  • rmdir will remove a directory at the specified path, BUT, rmdir if given a path to a file such as a .deb or .jar file will not know what to do.

  • rm -r or rm -rf will be able to completely terminate any file that you have permission to delete. I would wholeheartedly recommend NOT using the -f flag with rm, as even if you type a single character wrong, you can break your installation, something we don't want to happen.

Addressing your second question, I assume you are asking if it is necessary to include the path, and the answer to that is yes. Although commands can work on a local directory depending on your directory access in Terminal, using commands that can delete files is not a good idea in a local directory, because with one screw up, again, you can mess with your entire installation.

David
  • 3,367