Linux Command Reference ยท Files and Directories
Linux rmdir command
Reference for the Linux rmdir command: remove empty directories safely, remove a chain of parents with -p, and when to use rm -r instead.
rmdir removes directories, but only empty ones. That limit is the whole point: it is the safe way to delete a directory, because it refuses when there is anything inside and so can never destroy data by accident.
If you need to delete a directory together with its contents, use rm -r instead. A common pattern is to run rmdir for cleanup after moving files out, so that a leftover file stops the deletion and shows you a problem.
rmdir [OPTION]... DIRECTORY...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
rmdir [-pv] [--ignore-fail-on-non-empty] DIRECTORY...
| Option | Description |
|---|---|
-p, --parents |
Remove the directory, then try to remove each parent in the path (rmdir -p a/b/c is like rmdir a/b/c a/b a). |
-v, --verbose |
Print a message for every directory removed. |
--ignore-fail-on-non-empty |
Do not report an error just because a directory is not empty. |
Returns: 0 if every directory was removed, non-zero otherwise.
Common errors: "Directory not empty", "No such file or directory", "Not a directory" (you named a file, use rm), "Permission denied" (no write permission on the parent), "Invalid argument" (for . or ..), and "Device or resource busy" (a mount point or the current directory of some process).
Usage notes
- Hidden files count. A directory holding only
.gitkeepor.DS_Storeis not empty, sormdirfails; usels -A dirto see why. - Permission to delete a directory comes from the parent: you need write and execute on it. The directory itself may be mode 000.
rmdir -pstops at the first directory that cannot be removed, and then returns a non-zero status. It never removes/.- Symbolic links:
rmdir link/with a trailing slash tries to remove the target directory in some cases and fails with "Not a directory" in others. To remove a symlink to a directory userm link(no trailing slash). - Pruning empty trees:
find . -type d -empty -deleteremoves all empty directories, from the deepest up. It does not need-depthbecause-deleteimplies it. - In scripts,
rmdir dir 2>/dev/null || trueis a polite way to remove a temporary directory only when nothing else is in it. - Current directory: you cannot remove the directory you are standing in with
rmdir .; change out of it first.
Related
mkdir,rm,mv,find- Lessons: Copying, moving and deleting, Finding files