The system may always end up with empty directories usually after some projects. The rmdir is the safest command to remove those in Linux.
rmdir command in Linux is used to remove empty directories from the filesystem. By default, rmdir gives an error when it encounters a non-empty directory. Actions only on empty directories. It is simple and straightforward with only a few options.
Syntax
The basic syntax of rmdir command:
rmdir [options] <directory-name(s)>
Options
rmdir command is simple and straightforward with only a few options. The following table shows rmdir options:
Options | Description |
---|---|
-p | Remove parent directories |
--ignore-fail-on-non-empty | Ignore error output when deleting a non-empty directory |
-v, --verbose | Verbose information of the command |
Note: You need the necessary permissions to delete a directory. If you're getting a permission error, you may need to use sudo.
How to Use rmdir Command
To delete a single empty directory type rmdir followed by directory name.
For example to remove an empty directory named dir1, type:
rmdir dir1
The command will run successfully if the directory is empty.
If the directory is a non-empty directory it gives an error message saying 'rmdir: failed to remove 'Directory/' : Directory not empty'.
By default, for a successful rmdir run it won't display any output. You can use -v,--verbose option to output a diagnostic message for every directory processed.
rmdir -v dir5
To remove multiple empty directories, provide directory names as arguments separated with a space. For Example:
rmdir dir2 dir3 dir4
Use -p
option with rmdir command to remove a directory and its parent directories. Only works if all directories in the path are empty. For example
rmdir -p dir1/dir2/dir3
In the path, if any directory contains contents obviously the command will fail. This is because rmdir is designed to only remove empty directories. You can add the option `--ignore-fail-on-non-empty to tell rmdir command to proceed with other operations even when it encounters a non-empty directory.
rmdir -p --ignore-fail-on-non-empty dir1/dir2/dir3
Remember: This only ignores the alert and parent directories even if empty won't be deleted.
Comments