bug-coreutils@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: "Pádraig Brady" <P@draigBrady.com>
To: "Philip Rowlands" <phr+coreutils@dimebar.com>,
	"Łukasz Sroka" <sroka.dev@gmail.com>,
	60989@debbugs.gnu.org
Subject: bug#60989: [PATCH] rm: fail on duplicate input if force not enabled
Date: Sun, 22 Jan 2023 21:36:49 +0000	[thread overview]
Message-ID: <b39a9db9-e5c4-ed07-4fa8-f1323df5fedb@draigBrady.com> (raw)
In-Reply-To: <ac94672c-0d28-4fc1-8d65-1e119f30ff76@app.fastmail.com>

On 22/01/2023 18:18, Philip Rowlands wrote:
> 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.

That's informative, thanks.


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

Right with the -f option rm will not fail if no arguments are specified
(in the presence of nullglob etc.), which is POSIX compliant.

cheers,
Pádraig




      reply	other threads:[~2023-01-22 21:37 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-21 13:05 bug#60989: [PATCH] rm: fail on duplicate input if force not enabled Łukasz Sroka
2023-01-21 14:53 ` Pádraig Brady
2023-01-21 15:51   ` Łukasz Sroka
2023-01-21 18:51     ` Pádraig Brady
2023-01-22 18:18 ` Philip Rowlands
2023-01-22 21:36   ` Pádraig Brady [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-coreutils

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b39a9db9-e5c4-ed07-4fa8-f1323df5fedb@draigBrady.com \
    --to=p@draigbrady.com \
    --cc=60989@debbugs.gnu.org \
    --cc=phr+coreutils@dimebar.com \
    --cc=sroka.dev@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).