rmdir command in Linux with Examples



Name

rmdir - remove empty directories

Synopsis

rmdir [OPTION]... DIRECTORY...

Options

--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

Description

rmdir command removes empty directories specified on the command line. These directories are removed from the filesystem in the same order as they are specified on the command line, i.e., from left to right. If the directory is not empty you get an error message. You can specify multiple directories to be removed on the command line. It removes each directory specified on the command line in the order they are specified on the command line. If any of the directory is not empty, it displays an error message and proceeds to remove the next directory specified on the command line.

To remove a parent directory and a subdirectory of that parent, the subdirectory must be sepcified first, so the parent directory is empty when rmdir tries to remove it.

Examples

When the directory is not empty, rmdir command reports an error.

$ rmdir uifordata/
rmdir: failed to remove 'uifordata/': Directory not empty

To display verbose information for every directory being processed, we can use -v option.

$ rmdir -v socialdb/education/ socialdb/infotainment/ 
rmdir: removing directory, 'socialdb/education/'
rmdir: removing directory, 'socialdb/infotainment/'

To remove both a parent directory and a subdirectory of that parent, use -p option.

$ rmdir -v -p socialdb/entertainment 
rmdir: removing directory, 'socialdb/entertainment'
rmdir: removing directory, 'socialdb'

Normally, when rmdir command is being instructed to remove a non-empty directory, it simply reports an error. But you can use --ignore-fail-on-non-empty option to ignore these error messages.

Here socialdb directory has several files. After removing education folder, socialdb directory is still not empty. But the command ignores the error message.

$ rmdir --ignore-fail-on-non-empty  -p socialdb/education/
$	
Advertisements