Linux Commands – Difference Between rmdir and rm -r

commanddirectoryrm

I usually delete directories by using rm:

rm -r myDir

However I am aware of another command, rmdir, which seems to do the job just as well:

rmdir myDir

What is the difference between these two commands and when should each be used?

Best Answer

rm -r removes a directory and all its contents; rmdir will only remove a directory if the directory is empty. I like to use the following to remove a directory and all its contents:

rm -rf <directory_to_be_removed>