How to use rmdir command on Ubuntu Linux with examples

The rmdir command is used to remove empty directories on Linux systems.

For new users and students learning how to use and manage Linux systems, the easiest place to start might be on Ubuntu Linux OS.

Ubuntu is an open-source Linux operating systems that runs on desktops, laptops, server and other devices.

When you’re ready to learn how to use the rmdir commands, follow the guide below:

About rmdir command:

The rmdir command is used to remove empty directories on Linux systems. If the directory you wish to delete isn’t empty, the rmdir command will fail. It’s a same as using your mouse to right-click a directory or folder and select delete. the rmdir is the way to do it on the command line.

Only directories that have no files or other directories can be removed by using the rmdir command.

Syntax:

The syntax is the rule and format of how the rmdir command can be used. the systax options can be reordered. but a straight format must be followed.,.

Below is an example syntax of how to use the rmdir comamnd.

rmdir [OPTION]. DIRECTORY.

Options:

The command line options are switches or flags that determined how the commands are executed or controlled. they modify the behavior of the commands. they are separated by spaces and followed after the commands.

Below are some options of the rmdir command:

DIRECTORY. Replace DIRECTORY.. with the dorectory you want to remove or delete.
-p, –parents
remove DIRECTORY and its ancestors. e.g., ‘rmdir -p a/b/c’ is
similar to ‘rmdir a/b/c a/b a’
-v, –verbose            Use the -v or –verbose option output a diagnostic for every directory processed
–help Use the –help option to display this help and exit
 –version Use the –version option to output version information and exit

Examples:

Below are some examples of how to run and use the rmdir on Ubuntu Linux.

If you want to delete or remove an empty directory or folder called Confidential, you will run the commands below.

rmdir Confidential

To remove multiple empty directories or folders called Bank, Statements and Deposits, run the commands below.

rmdir Bank Statements Deposits

If you’re not logged in as a root account, you may have to use the sudo command it it.

sudo rmdir Bank Statements Deposits

To remove an empty sub directory, run the commands below.

sudo rmdir directory/sub_directory

When you run rmdir with the –help option, you’ll see the help text below:

Usage: rmdir [OPTION]. DIRECTORY.
Remove the DIRECTORY(ies), if they are empty.

      --ignore-fail-on-non-empty
                  ignore each failure that is solely because a directory
                    is non-empty
  -p, --parents   remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is
                    similar to 'rmdir a/b/c a/b a'
  -v, --verbose   output a diagnostic for every directory processed
      --help     display this help and exit
      --version  output version information and exit

That’s it!

Hope you like it and please come back soon for more Ubuntu Linux command!