bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#37686: Feature request: flag to not remove any files if any argument


From: Mark Friedenbach
Subject: bug#37686: Feature request: flag to not remove any files if any argument doesn't exist or is a directory
Date: Wed, 9 Oct 2019 14:01:57 -0700

I have a persistent problem that has caused me serious trouble in the
past: I mixup 'rm' and 'mv'. I suspect this is because on my keyboard
they are typed with the same fingers and muscle memory kicks in,
especially if I just typed the other command recently.

In any case something like "mv oldname newname" can be easily undone,
but "rm oldname newname" cannot. On my version of coreutils "rm
oldname newname" will report:

    rm: cannot remove 'newname': No such file or directory

...but it deletes "newname" anyway! The same goes for trying to move a
file into a directory, "rm filename dirname" which reports:

    rm: cannot remove 'dirname': Is a directory

...after deleting "filename". I have lost data on multiple occasions
this way. Today I went searching for an argument to add to 'rm' via an
alias to prevent this behavior and was very surprised to find there is
none.

There ought to be an option for GNU rm, let's call it '-n' or
'--no-clobber' but it could be named differently, which makes rm's
behavior all-or-nothing. If any errors which can be checked for
upfront are encountered (named file does not exist, file name
specifies a directory, etc.) then it aborts without doing anything.
Thus the expected behavior would be:

    $ alias rm='rm -n'
    $ ls
    $ touch filename
    $ ls
    filename
    $ rm filename newname
    rm: cannot remove 'newname': No such file or directory
    rm: aborting
    $ ls
    filename
    $ mkdir dirname
    dirname  filename
    $ rm filename dirname
    rm: cannot remove 'dirname': Is a directory
    rm: aborting
    $ ls
    dirname  filename
    $ rm -f filename dirname
    rm: cannot remove 'dirname': Is a directory
    $ ls
    dirname  filename

Adding '-f' / '--force' overrides this option:

    $ ls
    dirname  filename
    $ rm -f filename dirname
    rm: cannot remove 'dirname': Is a directory
    $ ls
    dirname
    $ rm -rf filename dirname
    rm: cannot remove 'filename': No such file or directory
    $ ls
    $





reply via email to

[Prev in Thread] Current Thread [Next in Thread]