Deleting or removing directories (rmdir command)

Use the rmdir command to remove the directory, specified by the Directory parameter, from the system.

The directory must be empty (it can contain only . and ..) before you can remove it, and you must have write permission in its parent directory. Use the ls -aDirectory command to check whether the directory is empty.
The following are examples of how to use the rmdir command:
  • To empty and remove a directory, type the following:
    rm mydir/* mydir/.* 
    rmdir mydir
    This removes the contents of mydir, then removes the empty directory. The rm command displays an error message about trying to remove the directories dot (.) and dot dot (..), and then the rmdir command removes them and the directory itself.
    Note: rm mydir/* mydir/.* first removes files with names that do not begin with a dot, and then removes those with names that do begin with a dot. The ls command does not list file names that begin with a dot unless you use the -a flag.
  • To remove the /tmp/jones/demo/mydir directory and all the directories beneath it, type the following:
    cd /tmp
    rmdir -p jones/demo/mydir
    This removes the jones/demo/mydir directory from the /tmp directory. If a directory is not empty or you do not have write permission to it when it is to be removed, the command terminates with appropriate error messages.