-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,890
  • 56
  • 97
  • 151
lad
  • 35

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.

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,487