Ubuntu Linux: Delete Directory Command in Terminal

How do I delete a directory in terminal using Ubuntu Linux operating systems?

You need to use the rmdir utility / command. The rmdir utility removes the directory entry specified by each directory argument, provided it is empty. Arguments are processed in the order given. In order to remove both a parent directory and a subdirectory of that parent, the subdirectory must be specified first so the parent directory is empty when rmdir tries to remove it.

[donotprint][/donotprint]Please note directory often referred to as a folder in the Apple Mac OS X and Microsoft Windows operating systems.

Syntax

The syntax is:

rmdir dirName

OR

rmdir [option] dirName

WARNING! These examples may crash your computer or may result into data loss, if executed without proper care.

Ubuntu delete directory called /tmp/foo

Open the terminal. Type the following command:
$ rmdir /tmp/foo
To remove foo and bar empty directories, type:
$ rmdir foo bar

Recursive directory removal on Ubuntu

Remove all files and directories including all sub-directories i.e. recursive removal:
$ rm -rf /path/to/directory
$ rm -rf /tmp/foo

Please note that you can also pass -p option to the rmdir command. Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component:
$ rmdir -p /tmp/x/y/z

Deleting directories as a superuser on ubuntu

If directory is owned by root or any other user or if you are getting “access denied/permission denied” message, try:

### Warning: careful with sudo and rm/rmdir command. ### 
### Check twice before you hit [enter] key ###
sudo rmdir /path/to/dir
sudo rm -rf /path/to/dir

### Warning: careful with sudo and rm/rmdir command. ###
### Check twice before you hit [enter] key ###
sudo rmdir /path/to/dir
sudo rm -rf /path/to/dir

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.