rmdir command is used remove empty directories from the filesystem in Linux. The rmdir command removes each and every directory specified in the command line only if these directories are empty. So if the specified directory has some directories or files in it then this cannot be removed by rmdir command.
rmdir [-p] [-v | –verbose] [–ignore-fail-on-non-empty] directories …
- –help: It will print the general syntax of the command along with the various options that can be used with the rmdir command as well as give a brief description about each option.
- rmdir -p: In this option each of the directory argument is treated as a pathname of which all components will be removed, if they are already empty, starting from the last component.
- rmdir -v, –verbose: This option displays verbose information for every directory being processed.
- rmdir –ignore-fail-on-non-empty: This option do not report a failure which occurs solely because a directory is non-empty. Normally, when rmdir is being instructed to remove a non-empty directory, it simply reports an error. This option consists of all those error messages.
- rmdir –version: This option is used to display the version information and exit.
Example 1: This will first remove the child directory and then remove the parent directory.
rmdir -p mydir/mydir1
Example 2: Remove the directories mydir1, mydir2, and mydir3, if they are empty. If any of these directories are not empty, then an error message will be printed for that directory, and the other directories will be removed.
rmdir mydir1 mydir2 mydir3
Example 3: Remove the directory mydir/mydir1 if it is empty. Then, remove directory mydir, if it is empty after mydir/mydir1 was removed.
rmdir mydir/mydir1 mydir