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 empty rmdir
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
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.
-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
andrmdir
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