diff options
author | Krzysztof Goj <krzysztof.goj@gmail.com> | 2012-01-22 01:39:59 +0100 |
---|---|---|
committer | Jim Meyering <meyering@redhat.com> | 2012-08-14 18:54:16 +0200 |
commit | fdc2da7165d93c8065365999341173ad2a818833 (patch) | |
tree | 25ef66176016b61abb71b56175b6cdb42fb54ee4 /src/rm.c | |
parent | 46afefaaa8ea95b5eb63a62792774cd18738234a (diff) | |
download | coreutils-fdc2da7165d93c8065365999341173ad2a818833.tar.gz |
rm: new option --dir (-d) to remove empty directories
Add new option to rm (-d/--dir), which allows removal of
empty directories, while still safely disallowing removal
of non-empty ones.
This improves compatibility with Mac OS X and BSD systems,
which honor the -d option.
* src/remove.c (rm_fts): Remove empty directories when requested.
* src/remove.h (rm_options) [remove_empty_directories]: New member.
* src/rm.c (long_opts, usage, main): Update usage and option parsing.
(rm_option_init): Initialize the new member.
* src/mv.c (rm_option_init): Initialize the new member.
* tests/rm/d-1: New test case - successfully delete empty dir.
* tests/rm/d-2: New test case - refuse to delete nonempty dir.
* tests/Makefile.am (TESTS): Add them.
Diffstat (limited to 'src/rm.c')
-rw-r--r-- | src/rm.c | 9 |
1 files changed, 8 insertions, 1 deletions
@@ -77,6 +77,7 @@ static struct option const long_opts[] = {"-presume-input-tty", no_argument, NULL, PRESUME_INPUT_TTY_OPTION}, {"recursive", no_argument, NULL, 'r'}, + {"dir", no_argument, NULL, 'd'}, {"verbose", no_argument, NULL, 'v'}, {GETOPT_HELP_OPTION_DECL}, {GETOPT_VERSION_OPTION_DECL}, @@ -154,6 +155,7 @@ Remove (unlink) the FILE(s).\n\ --no-preserve-root do not treat '/' specially\n\ --preserve-root do not remove '/' (default)\n\ -r, -R, --recursive remove directories and their contents recursively\n\ + -d, --dir remove empty directories\n\ -v, --verbose explain what is being done\n\ "), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); @@ -189,6 +191,7 @@ rm_option_init (struct rm_options *x) x->ignore_missing_files = false; x->interactive = RMI_SOMETIMES; x->one_file_system = false; + x->remove_empty_directories = false; x->recursive = false; x->root_dev_ino = NULL; x->stdin_tty = isatty (STDIN_FILENO); @@ -220,10 +223,14 @@ main (int argc, char **argv) /* Try to disable the ability to unlink a directory. */ priv_set_remove_linkdir (); - while ((c = getopt_long (argc, argv, "firvIR", long_opts, NULL)) != -1) + while ((c = getopt_long (argc, argv, "dfirvIR", long_opts, NULL)) != -1) { switch (c) { + case 'd': + x.remove_empty_directories = true; + break; + case 'f': x.interactive = RMI_NEVER; x.ignore_missing_files = true; |