[Top][All Lists]

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

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

From: Pádraig Brady
Subject: bug#60989: [PATCH] rm: fail on duplicate input if force not enabled
Date: Sun, 22 Jan 2023 21:36:49 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Thunderbird/109.0

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

   # 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.


reply via email to

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