GNU bug report logs - #60989
[PATCH] rm: fail on duplicate input if force not enabled

Previous Next

Package: coreutils;

Reported by: Łukasz Sroka <sroka.dev <at> gmail.com>

Date: Sat, 21 Jan 2023 14:29:02 UTC

Severity: normal

Tags: patch

Full log


Message #17 received at 60989 <at> debbugs.gnu.org (full text, mbox):

From: "Philip Rowlands" <phr+coreutils <at> dimebar.com>
To: Łukasz Sroka <sroka.dev <at> gmail.com>, 60989 <at> debbugs.gnu.org
Subject: Re: bug#60989: [PATCH] rm: fail on duplicate input if force not
 enabled
Date: Sun, 22 Jan 2023 18:18:04 +0000
On Sat, 21 Jan 2023, at 13:05, Łukasz Sroka wrote:
> When the input files contain duplicates, then the rm fails. Because
>     duplicates occur most often when the * is used and the shell unwraps it.
>     There is a very common scenario when a user accidentally enters space
>     after a filename, or enters space instead of forward slash.

To fail on duplicate FILE args, this bash function would do (lightly tested, doesn't attempt getopt processing):

function safe_rm {
  local -A seen
  local file
  for file in "$@"; do
    if [[ -v ${seen[$file]} ]]; then
      echo "error: duplicate name '$file'" 1>&2
      return 1
    fi
    seen[$file]=1
  done

  # no dupes seen
  command rm "$@"
}

and could be used today, without waiting for the next coreutils release.

As an aside, I could be reading it wrong but the coreutils manual suggests the file arguments are optional
   rm [option]… [file]…


Cheers,
Phil




This bug report was last modified 2 years and 150 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.