Linux rmdir command

Updated: 12/29/2017 by Computer Hope

About rmdir

The rmdir command removes empty directories from your filesystem.

Description

The rmdir command removes each directory specified on the command line, if they are empty. That is, each directory removed must contain no files or directories, or it cannot be removed by rmdir.

If any specified directory is not empty, rmdir will not remove it, and will proceed to try and remove any other directories you specified.

Directories are processed in the order you specify them on the command line, left to right.

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. (Or you can use the -p option; see below.)

rmdir is functionally equivalent to the command rm -d.

Note: If you want to remove a directory that is not empty (and also remove everything it contains), you can use the rm command with the -r (recursive) option. See the rm command for more information.

Syntax

rmdir [-p] [-v | --verbose] [--ignore-fail-on-non-empty] directory ...
rm --help
rm --version

Options

-p Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last component. (See rm for fully non-discriminant recursive removal.)
-v,
--verbose
Display verbose information for every directory processed.
--ignore-fail-on-non-empty Do not report a failure which occurs solely because a directory is non-empty. Normally, when rmdir is instructed to remove a non-empty directory, it reports an error. This option suppresses those error messages.
--help Display a help message, and exit.
--version Output version information, and exit.

Examples

rmdir mydir

Remove the directory mydir, if it is empty.

rmdir dir1 dir2 dir3

Remove the directories dir1, dir2, and dir3, if they are empty. If any are not empty, an error message will be printed for that directory, and the others will be removed.

rmdir dir/subdir dir

Remove the directory dir/subdir if it is empty. Then, remove directory dir, if it is empty after dir/subdir was removed.

rmdir -p dir/subdir

Same as the above command. rmdir attempts to remove dir/subdir, then attempts to remove dir.

mkdir — Create a directory.
rm — Delete files.