rmdirx
Remove the DIRECTORY(ies), if they are empty.
Mirrors GNU rmdir. Run it as rmdirx, or as gnux rmdirx.
Synopsis
rmdirx [OPTION]... DIRECTORY...GNU reference
Description
rmdirx removes each DIRECTORY, but only when it is empty: a directory that still has entries is reported and left untouched, and so is anything that is not a directory. That makes it the safe counterpart to a recursive delete - it can never destroy file contents. -p also removes each parent named in the operand, so 'rmdirx -p a/b/c' behaves like 'rmdirx a/b/c a/b a', stopping at the first one that cannot be removed. --ignore-fail-on-non-empty silences the not-empty failure (and only that one), and -v reports every directory processed. On a terminal, gnux's terminal view colors the -v report: the verb (removed, created, renamed), each quoted name and the arrows, in the theme --theme or GNUX_THEME names; the words are GNU's, and piped or redirected output is never colored (--pretty=never or GNUX_PRETTY=never turns it off).
Permissions
Removing a directory needs write and search permission on its parent, not on the directory itself. A symbolic link to a directory is not a directory: it is refused rather than followed.
Use cases
- Removing a directory while guaranteeing nothing inside it is lost.
- Pruning a chain of now-empty parents after moving files out with
-p. - Best-effort cleanup in scripts with
--ignore-fail-on-non-empty. - Checking that a directory really is empty:
rmdirxfails, and says why, if it is not. - Tidying up the empty leftovers of a build or an extraction with find.
- Undoing a '
mkdirx -p' of a tree that never received any files. - Auditing a cleanup with
-v, which names every directory removed.
Flags
--help display this help and exit
--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., 'rmdirx -p a/b/c' is similar to 'rmdirx a/b/c a/b a'
--pretty WHEN[="always"] the gnux terminal view WHEN: 'always' (the default when WHEN is omitted), 'auto' (the default: on a terminal only), or 'never'; GNUX_PRETTY sets the default
--theme NAME color the view with the built-in theme NAME (gnux theme picks and saves one); GNUX_THEME sets the default
-v, --verbose output a diagnostic for every directory processed
--version output version information and exitExamples
Remove an empty directory
rmdirx buildRemove several empty directories at once
rmdirx a b cReport each directory as it is removed
rmdirx -v empty1 empty2Remove a directory and its now-empty parents, reporting each
rmdirx -pv out/reports/2024Remove a chain of parents and confirm the top one is gone
rmdirx -p x/y/z && echox 'x is gone'Clean up whatever is empty without failing on the rest
rmdirx --ignore-fail-on-non-empty cache logs tmpPrune parents as far as they are empty, stopping quietly at the first that is not
rmdirx -p --ignore-fail-on-non-empty keep/subRefuse to remove a directory that still has files (exit status 1)
rmdirx logs; echox "exit $?"Use the failure as an emptiness test
rmdirx logs 2>/dev/null || echox 'logs still has files'Remove every empty directory below a tree, deepest first
find tree -depth -type d -empty -exec rmdirx -v {} \;Create and then remove a temporary staging tree with the gnux shell
gnux shell -c 'mkdirx -p staging/a && rmdirx -pv staging/a'Options
Each option has a page of its own, with its usage and examples.
- --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., '
rmdirx -pa/b/c' is similar to 'rmdirxa/b/c a/b a'. - --pretty: The
gnuxterminal view WHEN: 'always' (the default when WHEN is omitted), 'auto' (the default: on a terminal only), or 'never'; GNUX_PRETTY sets the default. - --theme: Color the view with the built-in theme NAME (
gnuxtheme picks and saves one); GNUX_THEME sets the default. - -v, --verbose: Output a diagnostic for every directory processed.
Exit status
| Status | Meaning |
|---|---|
| 0 | success: every operand was processed |
| 1 | failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake |
Help in the terminal
rmdirx --help # options, first examples, exit status
gnux help rmdirx # this page: the full manual
gnux help rmdirx --OPTION # one option in depth, with its examples