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?
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?
rmdir
can only remove empty directoriesrm -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 emptyrmdir
will fail. It is a good practice that will prevent unwanted deletion, hidden files for example.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
orrm -rf
will be able to completely terminate any file that you have permission to delete. I would wholeheartedly recommend NOT using the-f
flag withrm
, 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.