rmdir
rmdir
The rmdir
command in Unix and Linux is used to remove empty directories (folders) from the file system. It is specifically designed to delete directories that contain no files or subdirectories.
Basic Usage
The basic syntax for the rmdir
command is:
rmdir [options] directory...
options
: Optional command-line options to control the behavior ofrmdir
.directory
: The name(s) of the directory(ies) to be removed.
Examples
Removing an Empty Directory
To remove an empty directory:
rmdir emptydir
This command deletes the directory named emptydir
from the current working directory. It will only work if emptydir
is empty (contains no files or subdirectories).
Removing Multiple Empty Directories
To remove multiple empty directories at once:
rmdir dir1 dir2 dir3
This command removes the empty directories dir1
, dir2
, and dir3
from the current working directory.
Options
-p
Option: Remove Parent Directories
To remove a directory and its empty parent directories:
rmdir -p path/to/emptydir
This command removes emptydir
and its parent directories if they are empty.
-v
Option: Verbose Output
To display detailed information about the directories being removed:
rmdir -v emptydir
This command provides verbose output, indicating the directory emptydir
was successfully removed.
Practical Use Cases
Cleaning Up Temporary Directories
To clean up temporary directories that are no longer needed:
rmdir tempdir1 tempdir2 tempdir3
This command removes multiple temporary directories that have served their purpose and are now empty.
Batch Processing with find
To remove all empty directories within a specific path:
find /path/to/search -type d -empty -exec rmdir {} \;
This command uses find
to locate all empty directories (-type d -empty
) under /path/to/search
and executes rmdir
on each one.
Safety Tips
Verify Directory Contents: Ensure directories you intend to delete are indeed empty to avoid unintentional data loss.
Use with Caution:
rmdir
deletes directories permanently and cannot be undone, so exercise caution when using it.
Summary
The rmdir
command is a straightforward tool for deleting empty directories in Unix and Linux environments. Its simplicity and optional flags provide flexibility for efficiently cleaning up directories that are no longer needed. Understanding its usage and options ensures safe and effective directory management on your system.
help
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
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
Full documentation <https://www.gnu.org/software/coreutils/rmdir>
or available locally via: info '(coreutils) rmdir invocation'
Last updated