GNU bug report logs -
#37686
Feature request: flag to not remove any files if any argument doesn't exist or is a directory
Previous Next
To reply to this bug, email your comments to 37686 AT debbugs.gnu.org.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
bug-coreutils <at> gnu.org
:
bug#37686
; Package
coreutils
.
(Wed, 09 Oct 2019 21:59:02 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
Mark Friedenbach <mark <at> friedenbach.org>
:
New bug report received and forwarded. Copy sent to
bug-coreutils <at> gnu.org
.
(Wed, 09 Oct 2019 21:59:02 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
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
$
Information forwarded
to
bug-coreutils <at> gnu.org
:
bug#37686
; Package
coreutils
.
(Mon, 14 Oct 2019 19:36:02 GMT)
Full text and
rfc822 format available.
Message #8 received at 37686 <at> debbugs.gnu.org (full text, mbox):
On 2019/10/09 14:01, Mark Friedenbach wrote:
> 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.
>
rm () {
if [[ -t 0 ]]; then echo >&2 Disallowed;return 1; fi
command "$FUNCNAME" "$@"
}
mv () {
if [[ -t 0 ]]; then echo >&2 Disallowed;return 1; fi
command "$FUNCNAME" "$@"
}
alias move='</dev/null mv'
alias remove='</dev/null rm'
How about something _like_ the above (spur of the moment idea,
written in 'bash'...
Interactively, you type 'move' or 'remove'
(or whatever you want to name them). But scripts you run
won't be affected.
Might have to adjust for your use case, but might protect against
most cases.
Nightly backups also help prevent catastrophic problems.
At the same time, with functions or aliases, you could test for
the existence of the arguments before actually executing them.
This bug report was last modified 5 years and 310 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.