From unknown Sun Aug 17 10:19:17 2025 X-Loop: help-debbugs@gnu.org Subject: bug#60989: [PATCH] rm: fail on duplicate input if force not enabled Resent-From: =?UTF-8?Q?=C5=81ukasz?= Sroka Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Sat, 21 Jan 2023 14:29:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 60989 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: patch To: 60989@debbugs.gnu.org X-Debbugs-Original-To: bug-coreutils@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.167431129331555 (code B ref -1); Sat, 21 Jan 2023 14:29:02 +0000 Received: (at submit) by debbugs.gnu.org; 21 Jan 2023 14:28:13 +0000 Received: from localhost ([127.0.0.1]:48377 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJEqr-0008Ct-3L for submit@debbugs.gnu.org; Sat, 21 Jan 2023 09:28:13 -0500 Received: from lists.gnu.org ([209.51.188.17]:36020) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJDYz-0006Dp-Uk for submit@debbugs.gnu.org; Sat, 21 Jan 2023 08:05:42 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pJDYz-00006v-J4 for bug-coreutils@gnu.org; Sat, 21 Jan 2023 08:05:41 -0500 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pJDYx-0004gu-OA for bug-coreutils@gnu.org; Sat, 21 Jan 2023 08:05:41 -0500 Received: by mail-pf1-x42e.google.com with SMTP id c26so5844330pfp.10 for ; Sat, 21 Jan 2023 05:05:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=NxxCzssBD0cIWcnLLssSDbBmG2H3TOUJCGa0t8Dn0oI=; b=TUEwOmhC/Scd4FL0OydFEIGgQ/CfmzHfwvfNGEwXNoHSn5iR4F00glNAJbQz4oqVIY LRW/0PmP+ep6iqvxmo/Wk7rDF7x0oYesJDde61axxylyqJxu6QQzGEQhtukZwab3bSX7 szCyTaEPjgRnft96BwrVzF9qONRlw2u0oLE0JPGZOo4dRd6W23TaC4F95+ADK3KjpS0Y JRwwquET9Td+817yli1McNqmxGJ19yNTwyTrZrdW2yOe1QR4niEQuKL+5A1zXE78hKWz zZF2/0F+6lP89U9if5xSW0zEVOGRZBRPMLF2ydyjm/0n9lYEROYoijbdNIJzyyzB6m3U QY1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=NxxCzssBD0cIWcnLLssSDbBmG2H3TOUJCGa0t8Dn0oI=; b=78uSW2oKUukWJRHmuwouxRPIxcAXn7GWQEuj+EcJFZNWdVRMizzMRLJgYbLT961Fva xIjibS9NmU5lAv3HUpnh051B4mw87Nb6Eje4paogGKJEHswWof88P4dSuqPC5hXedWrk 3fvIlMUpSeeeFueObiRtictjCkaMXiPIQUSnoxh5vm1QymW4zIaOAoNWqYSZIe1JSomN Zd21JsBJWpW7RmVpXaYZZOgdtBc0PWXKm35rX4lX12lU/ttV0XVWBYMr2tCPy3at1zhC MTHwtJnzG8+P5tcUJizA41n0doR6PclaDXZB8WBXOHYVx3FhshfP6iRMK0rZ55eWtTFt O8FQ== X-Gm-Message-State: AFqh2kpAkPeOwDhtgMSAwqWytk0Cd6O9uMrFv0PVyfx3ehUrCuXXt7Hm 6U+jmDra6Xoo/UzByEOYCIF+59TofD9/HH2yLJQkufrCX0E= X-Google-Smtp-Source: AMrXdXsR0CpADgpH8WunKEGtZlyFChIuhHFUl9imzPuxoTx1/t0xwIEv86hCu1hRt6n+jkxsr1QU9TWPjXbsQgm6jLk= X-Received: by 2002:aa7:820f:0:b0:58d:a713:d1dd with SMTP id k15-20020aa7820f000000b0058da713d1ddmr1970474pfi.59.1674306337429; Sat, 21 Jan 2023 05:05:37 -0800 (PST) MIME-Version: 1.0 From: =?UTF-8?Q?=C5=81ukasz?= Sroka Date: Sat, 21 Jan 2023 14:05:25 +0100 Message-ID: Content-Type: text/plain; charset="UTF-8" Received-SPF: pass client-ip=2607:f8b0:4864:20::42e; envelope-from=sroka.dev@gmail.com; helo=mail-pf1-x42e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-Mailman-Approved-At: Sat, 21 Jan 2023 09:28:12 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.3 (--) 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. Example: rm prefix_ * The user intended to remove all files with a `prefix_` but removed all of the files in cwd. The program quits immediately when a duplicate is detected, to prevent pressing `y` because user expected a prompt regarding removing multiple files. The force option disables this function to enable scripts to work without modifying them. ``` diff --git a/src/rm.c b/src/rm.c index 354e2b0df..e4f9949f0 100644 --- a/src/rm.c +++ b/src/rm.c @@ -123,6 +123,16 @@ diagnose_leading_hyphen (int argc, char **argv) } } +static bool +find_duplicates (int n_files, char **files) +{ + for (int l = 0; l < n_files-1; l++) + for (int r = l+1; r < n_files; r++) + if (strcmp(files[l], files[r]) == 0) + return true; + return false; +} + void usage (int status) { @@ -211,6 +221,7 @@ main (int argc, char **argv) bool preserve_root = true; struct rm_options x; bool prompt_once = false; + bool force_rm = false; int c; initialize_main (&argc, &argv); @@ -238,6 +249,7 @@ main (int argc, char **argv) x.interactive = RMI_NEVER; x.ignore_missing_files = true; prompt_once = false; + force_rm = true; break; case 'i': @@ -352,6 +364,17 @@ main (int argc, char **argv) uintmax_t n_files = argc - optind; char **file = argv + optind; + if (!force_rm && find_duplicates(n_files, file)) + { + /* Because usually when the input files are duplicated it means that the user + sumbitted both a directory and an * as separate arguments, probably by accident */ + fprintf (stderr, + "%s: input contains duplicates, most likely you've put " + "both * and a file from the same directory.\n", + program_name); + return EXIT_FAILURE; + } + if (prompt_once && (x.recursive || 3 < n_files)) { fprintf (stderr, ``` From unknown Sun Aug 17 10:19:17 2025 X-Loop: help-debbugs@gnu.org Subject: bug#60989: [PATCH] rm: fail on duplicate input if force not enabled Resent-From: =?UTF-8?Q?P=C3=A1draig?= Brady Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Sat, 21 Jan 2023 14:54:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60989 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: patch To: =?UTF-8?Q?=C5=81ukasz?= Sroka , 60989@debbugs.gnu.org Received: via spool by 60989-submit@debbugs.gnu.org id=B60989.16743128041890 (code B ref 60989); Sat, 21 Jan 2023 14:54:02 +0000 Received: (at 60989) by debbugs.gnu.org; 21 Jan 2023 14:53:24 +0000 Received: from localhost ([127.0.0.1]:48420 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJFFD-0000UP-Ia for submit@debbugs.gnu.org; Sat, 21 Jan 2023 09:53:23 -0500 Received: from mail-wr1-f45.google.com ([209.85.221.45]:44854) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJFFB-0000UC-5U for 60989@debbugs.gnu.org; Sat, 21 Jan 2023 09:53:21 -0500 Received: by mail-wr1-f45.google.com with SMTP id bk16so7157165wrb.11 for <60989@debbugs.gnu.org>; Sat, 21 Jan 2023 06:53:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :sender:from:to:cc:subject:date:message-id:reply-to; bh=uMV8nSBhVYM+JyJJ4FDVbY7NULb1N2UYJ6exVSz/K/E=; b=FWAVkS5cjLONrbDcBotEOl7JUcQkIiQkge6uIKKJGxTm4lKHlbHLOSMFWFlFs0QQSB LUH90xut9NvVryHj0KNMxERGs9U/z2eobiG8+aV9UR81jvNNwt9k+FjEK6J261+d5Zci yA/YtpPbpfA8k9CfEC9EwRjBq2ml2ES894dm4abUqsmzaCjkDtGqoggcC/xarsUaT3YI IbNiuKlCID0rtMn5qkCUsteN51OAqivGnGkBDZy5vGNULl3oTp8YR35YqdrFEJDCI7NN QNBRa3rky74ZEiEeEAoXhsWUApmQZjY8k9oxHCfvwX1JkLT+HgqtID8s1MwVxYLFx0sQ BBwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :sender:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uMV8nSBhVYM+JyJJ4FDVbY7NULb1N2UYJ6exVSz/K/E=; b=iAyCCpYCwpNFrrfGtnhCnU8lp4x+M+0+p4L3japZ+oUg+u7ujMHx0vBYcqTVrD3iCR hQPOQHmoYOl21ZOTSvhBzHpv8d3sYo/Xu4GxAie5tdljj796c4Fon/y2Fjdzxgws1H3s OJYNOXMpeMs6RSzVsKo1UWuZy9QSha/XUlvkOCJxAyrmSTDxZs3ifAmzOybDbL/Ir/f6 6ss2ahaA7IWeYaC9+16HgCtBOWo/E4IBEVHg+t2WJrAPRlVGNtHu8nJWPmC6fXJdJWGJ LYm1UkUY0Ls+dPru3SxX0Gq/1HAqVRtx4JHOim8tuzxmxF4KWJv6u9vDjsVMkeuEQHGv u9ew== X-Gm-Message-State: AFqh2kpqrvIXYzbDvETyhueVpMn0D+3MaHeumZcRpjx8kIrTWYvT4yXv v+C8i9Nn+JOp3BHKf9DZso0= X-Google-Smtp-Source: AMrXdXsoqf6qlSOtKRXwHyG3BqA9OYUWxFAWh7z/xAvqwnTckvEL06uDYvV4CIoFN+8cwlvGhtTbbA== X-Received: by 2002:adf:f54d:0:b0:273:7d1b:7337 with SMTP id j13-20020adff54d000000b002737d1b7337mr25049079wrp.7.1674312795195; Sat, 21 Jan 2023 06:53:15 -0800 (PST) Received: from [192.168.1.9] (95-44-90-175-dynamic.agg2.lod.rsl-rtd.eircom.net. [95.44.90.175]) by smtp.googlemail.com with ESMTPSA id c6-20020adffb06000000b002423620d356sm18196936wrr.35.2023.01.21.06.53.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 21 Jan 2023 06:53:14 -0800 (PST) Message-ID: <0461e966-8ae5-4fcb-7f9f-42283f6b30cb@draigBrady.com> Date: Sat, 21 Jan 2023 14:53:13 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Thunderbird/109.0 Content-Language: en-US References: From: =?UTF-8?Q?P=C3=A1draig?= Brady In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Score: -0.9 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.9 (-) On 21/01/2023 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. > Example: > > rm prefix_ * > > The user intended to remove all files with a `prefix_` but removed all > of the files in cwd. > The program quits immediately when a duplicate is detected, to prevent > pressing `y` because user expected a prompt regarding removing multiple > files. > The force option disables this function to enable scripts to work > without modifying them. > > ``` > diff --git a/src/rm.c b/src/rm.c > index 354e2b0df..e4f9949f0 100644 > --- a/src/rm.c > +++ b/src/rm.c > @@ -123,6 +123,16 @@ diagnose_leading_hyphen (int argc, char **argv) > } > } > > +static bool > +find_duplicates (int n_files, char **files) > +{ > + for (int l = 0; l < n_files-1; l++) > + for (int r = l+1; r < n_files; r++) > + if (strcmp(files[l], files[r]) == 0) > + return true; > + return false; > +} > + > void > usage (int status) > { > @@ -211,6 +221,7 @@ main (int argc, char **argv) > bool preserve_root = true; > struct rm_options x; > bool prompt_once = false; > + bool force_rm = false; > int c; > > initialize_main (&argc, &argv); > @@ -238,6 +249,7 @@ main (int argc, char **argv) > x.interactive = RMI_NEVER; > x.ignore_missing_files = true; > prompt_once = false; > + force_rm = true; > break; > > case 'i': > @@ -352,6 +364,17 @@ main (int argc, char **argv) > uintmax_t n_files = argc - optind; > char **file = argv + optind; > > + if (!force_rm && find_duplicates(n_files, file)) > + { > + /* Because usually when the input files are duplicated it means > that the user > + sumbitted both a directory and an * as separate arguments, > probably by accident */ > + fprintf (stderr, > + "%s: input contains duplicates, most likely you've put " > + "both * and a file from the same directory.\n", > + program_name); > + return EXIT_FAILURE; > + } > + > if (prompt_once && (x.recursive || 3 < n_files)) > { > fprintf (stderr, > ``` An interesting proposal. The main protection would be for `dir/ *` rather than `file_prefix_ *`. The former would be unusual for a user to type, while the latter more usual, but wouldn't trigger the protection AFAICS. This ads O(N^2) on each interaction, so if it was to be included probably only enabled with --interactive. cheers, Pádraig From unknown Sun Aug 17 10:19:17 2025 X-Loop: help-debbugs@gnu.org Subject: bug#60989: [PATCH] rm: fail on duplicate input if force not enabled Resent-From: =?UTF-8?Q?P=C3=A1draig?= Brady Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Sat, 21 Jan 2023 18:52:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60989 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: patch To: =?UTF-8?Q?=C5=81ukasz?= Sroka Cc: 60989@debbugs.gnu.org Received: via spool by 60989-submit@debbugs.gnu.org id=B60989.167432712212079 (code B ref 60989); Sat, 21 Jan 2023 18:52:02 +0000 Received: (at 60989) by debbugs.gnu.org; 21 Jan 2023 18:52:02 +0000 Received: from localhost ([127.0.0.1]:49756 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJIy9-00038g-IS for submit@debbugs.gnu.org; Sat, 21 Jan 2023 13:52:02 -0500 Received: from mail-wm1-f47.google.com ([209.85.128.47]:36640) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJIy7-00038M-4j for 60989@debbugs.gnu.org; Sat, 21 Jan 2023 13:52:00 -0500 Received: by mail-wm1-f47.google.com with SMTP id c4-20020a1c3504000000b003d9e2f72093so7968747wma.1 for <60989@debbugs.gnu.org>; Sat, 21 Jan 2023 10:51:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :sender:from:to:cc:subject:date:message-id:reply-to; bh=F6OGjq4ho6ZA+YfRSs/TJPIbW73OaeTfTMm+cHuCot8=; b=HrCjDeEG3HxRbxPRBtX+HX6VAVlEHtbPaPFlEJASSscAltBN9xBQZAMBuyCNOrfZCB XGeCrFSdeNS7YkPKwa6cw4qf0NpF/WU5C6/0OEwLX51299CmxhjmIUHi56CrWwXtc76H +sRmPkyT7vh2F3cV+30397GTc9U1f5e45KQJ6U4zn3cWLKWc8MKDRQTPm+gjByTKHUPI THcEtzdaDGl/C1DQMOxY3rbR3vDBnjjPmk3FC4+kwYfxO5HbskDquOUCHnDkgTRXEUY+ fgY497TNCkNJUvnHili4CkgmPGbwtM/sMTa8FpqEtiFvU1mAgGP4X4Q4nSCWGRaIzpvg oylw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :sender:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=F6OGjq4ho6ZA+YfRSs/TJPIbW73OaeTfTMm+cHuCot8=; b=X6ooCGyhdNJy4VZ6UAIqG0UB1/3Vq/r7OKlVuoq+rDLwmTBMBGtcOMmylsjo0NNPzK TqAf/QDv0viTtx93wUfluux/vSibKjtCTPvzznZQEwY26VCuR7TIKmcZCOdvdcAX40Rr luZ/Q4MkfWe0acIf0PBj7mWbuhIalPQ+2/YsGSmOgYHqEXezqJjVXqtsbsbdtOyhNrBt IAPEvd8y5XSHL/I9Wb//l7aWpZnTjSVDdUwFOZdp16YtgqJGUox/UbCfAnceV6agB/eS 1gZBURQCNMRAMBFcUiRxSkm2ov6qD9U7+CEnHdRfB3syZLVznXGNDMn4RsNf2TyrANV4 RTiA== X-Gm-Message-State: AFqh2krfA0m7wnOhPuOBTLXOvzMlK0euYJ771i08td7jB7g6zqNChDti 9jRIBzCeSnzKqrMshHTcoCY= X-Google-Smtp-Source: AMrXdXtvNl9UEMHXnqe+bVUmIt3GxpK1Yz/SqQNFdCug3RgM3LroSaMQg/rbjDWVvM2QQ0zWtytj0w== X-Received: by 2002:a05:600c:2e8a:b0:3da:2a78:d7b4 with SMTP id p10-20020a05600c2e8a00b003da2a78d7b4mr19326897wmn.3.1674327112989; Sat, 21 Jan 2023 10:51:52 -0800 (PST) Received: from [192.168.1.9] (95-44-90-175-dynamic.agg2.lod.rsl-rtd.eircom.net. [95.44.90.175]) by smtp.googlemail.com with ESMTPSA id c2-20020a05600c0a4200b003daf6e3bc2fsm16082404wmq.1.2023.01.21.10.51.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 21 Jan 2023 10:51:52 -0800 (PST) Message-ID: <8de654c7-f1d2-aa18-a316-8f931f352a54@draigBrady.com> Date: Sat, 21 Jan 2023 18:51:51 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Thunderbird/109.0 Content-Language: en-US References: <0461e966-8ae5-4fcb-7f9f-42283f6b30cb@draigBrady.com> From: =?UTF-8?Q?P=C3=A1draig?= Brady In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Score: -0.9 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.9 (-) On 21/01/2023 15:51, Łukasz Sroka wrote: > On 21/01/2023 15:53, Pádraig Brady wrote: >> >> On 21/01/2023 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. >>> Example: >>> >>> rm prefix_ * >>> >>> The user intended to remove all files with a `prefix_` but removed all >>> of the files in cwd. >>> The program quits immediately when a duplicate is detected, to prevent >>> pressing `y` because user expected a prompt regarding removing multiple >>> files. >>> The force option disables this function to enable scripts to work >>> without modifying them. >>> >>> ``` >>> diff --git a/src/rm.c b/src/rm.c >>> index 354e2b0df..e4f9949f0 100644 >>> --- a/src/rm.c >>> +++ b/src/rm.c >>> @@ -123,6 +123,16 @@ diagnose_leading_hyphen (int argc, char **argv) >>> } >>> } >>> >>> +static bool >>> +find_duplicates (int n_files, char **files) >>> +{ >>> + for (int l = 0; l < n_files-1; l++) >>> + for (int r = l+1; r < n_files; r++) >>> + if (strcmp(files[l], files[r]) == 0) >>> + return true; >>> + return false; >>> +} >>> + >>> void >>> usage (int status) >>> { >>> @@ -211,6 +221,7 @@ main (int argc, char **argv) >>> bool preserve_root = true; >>> struct rm_options x; >>> bool prompt_once = false; >>> + bool force_rm = false; >>> int c; >>> >>> initialize_main (&argc, &argv); >>> @@ -238,6 +249,7 @@ main (int argc, char **argv) >>> x.interactive = RMI_NEVER; >>> x.ignore_missing_files = true; >>> prompt_once = false; >>> + force_rm = true; >>> break; >>> >>> case 'i': >>> @@ -352,6 +364,17 @@ main (int argc, char **argv) >>> uintmax_t n_files = argc - optind; >>> char **file = argv + optind; >>> >>> + if (!force_rm && find_duplicates(n_files, file)) >>> + { >>> + /* Because usually when the input files are duplicated it means >>> that the user >>> + sumbitted both a directory and an * as separate arguments, >>> probably by accident */ >>> + fprintf (stderr, >>> + "%s: input contains duplicates, most likely you've put " >>> + "both * and a file from the same directory.\n", >>> + program_name); >>> + return EXIT_FAILURE; >>> + } >>> + >>> if (prompt_once && (x.recursive || 3 < n_files)) >>> { >>> fprintf (stderr, >>> ``` >> >> An interesting proposal. >> The main protection would be for `dir/ *` rather than `file_prefix_ *`. >> The former would be unusual for a user to type, while the latter more usual, but wouldn't trigger the protection AFAICS. >> This ads O(N^2) on each interaction, so if it was to be included probably only enabled with --interactive. >> >> cheers, >> Pádraig > > Yeah, true. Implemented it quickly and focused on the `dir/ *` > scanerio at first, because that has happened to me -.- > It was because I tried to do something quickly while editing command > from backsearch and ended up with `rm tmp/ *` instead of `rm tmp/*`. > To make the prefix_ protection it won't be possible without checking > if a "prefix" exits on disk (eg. file and file1), which would be more > time complex. > I don't like the --interactive approach, as when you're trying to do > something quickly, you probably won't opt for doing it interactively > and you're probably either disabled or ignoring shell warnings. True, but at a high level -I can be seen as an unintrusive "be more careful" option, and so is often enabled by default with an alias. Red Hat flavored systems even enable the more intrusive -i option by default for the root user for example. > The more I think about this problem, the more I tilt towards > implementing it in shell. If you'd've written "rm prefix_ *" and had > 10k files there, it would be so much easier to detect it on the shell > side than checking every filename with the other and triggering > syscalls for every one... > Either way if you have other solutions in mind, please update me, as > it wouldn't require to configure shell in dockers or remote machines, > thus making the solution more available. A shell option like `shopt -s uniqueglob` would indeed be more general. There are various other globbing options, like the similar "failglob" which fails to execute the command if a glob doesn't match anything. cheers, Pádraig From unknown Sun Aug 17 10:19:17 2025 X-Loop: help-debbugs@gnu.org Subject: bug#60989: [PATCH] rm: fail on duplicate input if force not enabled Resent-From: =?UTF-8?Q?=C5=81ukasz?= Sroka Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Sat, 21 Jan 2023 19:20:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60989 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: patch To: =?UTF-8?Q?P=C3=A1draig?= Brady Cc: 60989@debbugs.gnu.org Received: via spool by 60989-submit@debbugs.gnu.org id=B60989.167432879416002 (code B ref 60989); Sat, 21 Jan 2023 19:20:01 +0000 Received: (at 60989) by debbugs.gnu.org; 21 Jan 2023 19:19:54 +0000 Received: from localhost ([127.0.0.1]:49800 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJJP7-0004A1-LU for submit@debbugs.gnu.org; Sat, 21 Jan 2023 14:19:54 -0500 Received: from mail-pl1-f179.google.com ([209.85.214.179]:34402) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJG9d-0004k6-UL for 60989@debbugs.gnu.org; Sat, 21 Jan 2023 10:51:42 -0500 Received: by mail-pl1-f179.google.com with SMTP id v23so7829965plo.1 for <60989@debbugs.gnu.org>; Sat, 21 Jan 2023 07:51:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=d1fXG1hXAO7x9RNAUUZZEJ+9sBl9UGNQ96A6BHsh7q0=; b=jUIrsXpZXhD7OQ5uXIkeDjAdAtlqUFe4T7+VQeDXEBAHbUelcqBzssSXbALPhaWbfV GrI2Ff1Zzh4Be1un8cvY5dQ4QoEHlNY0YFWZ96hm0nb5Mb25uNo6SSko92IL8HZhmxJX n02L6JvKBHNlfYMdN1pJhaT+uSCxqClDln7fod5EZyvmAQSAOrWBG/IwLKzdeXTZpmxz 6jgm1f9VZDjqKHHewaG6UoFp/poawZRi4RqACM+FCJaEyCYxyxaV0aaWILu1iXF1P8Hq hOqHbjw6zL7V4P8CevQPbeOebPVdX7jGo16t7ic6Clmz9TD9RxjJoZ2XumACzLnYw7mU pHLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=d1fXG1hXAO7x9RNAUUZZEJ+9sBl9UGNQ96A6BHsh7q0=; b=5zSNqm9iErwHk/A38IUSgAnrXnyiviQAnS1rcAmaRN0wNjZJ2+nNRzp55467FWFWdY L714kLHLBAsWnWEtXwar9t9iZB+AOEh65IExXcSDPsPxxFOPTiSdTk+E5yFxnpc/5G+e FUxn0njp+Dv8H6g8aK020yM8CEUZ4zFL0U26QRzwlLHbDa9Nc88OMEutfOMRCHBHXdRk 8gu8bh2OYDIJ4AFbERGEo6iSI+bPXqv9V4ss6d+hF2YJXNHsVRzQGf4P8Y70qafpyvcK Ip0Vp0poaXIwTctKFrTtgMAeiyBc/BKompZFV/90swuMPFzN2jQRFiQzraQMIdIWsfZX 2/qw== X-Gm-Message-State: AFqh2kovXK8Cp8+8ExkAfR/X8hyQDrS86S2rFApCkTlrMCXVRKEZsaqQ 8YtaqphtF4SMjq38KDmG8Dp9jhImDsikyCejRMM= X-Google-Smtp-Source: AMrXdXvTUqvEakNIPYJw59ZzXX02mIFGO+1NtQTWgoZUFKfFpAjhI0dhQJYBnYVueiY2QMTMMgx4irr0F34coiSX/Yg= X-Received: by 2002:a17:90a:d50c:b0:229:4385:1b54 with SMTP id t12-20020a17090ad50c00b0022943851b54mr2651384pju.118.1674316295698; Sat, 21 Jan 2023 07:51:35 -0800 (PST) MIME-Version: 1.0 References: <0461e966-8ae5-4fcb-7f9f-42283f6b30cb@draigBrady.com> In-Reply-To: <0461e966-8ae5-4fcb-7f9f-42283f6b30cb@draigBrady.com> From: =?UTF-8?Q?=C5=81ukasz?= Sroka Date: Sat, 21 Jan 2023 16:51:23 +0100 Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Score: 0.0 (/) X-Mailman-Approved-At: Sat, 21 Jan 2023 14:19:53 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) On 21/01/2023 15:53, P=C3=A1draig Brady wrote: > > On 21/01/2023 13:05, =C5=81ukasz Sroka wrote: > > When the input files contain duplicates, then the rm fails. Becaus= e > > duplicates occur most often when the * is used and the shell unwra= ps it. > > There is a very common scenario when a user accidentally enters sp= ace > > after a filename, or enters space instead of forward slash. > > Example: > > > > rm prefix_ * > > > > The user intended to remove all files with a `prefix_` but removed= all > > of the files in cwd. > > The program quits immediately when a duplicate is detected, to pre= vent > > pressing `y` because user expected a prompt regarding removing mul= tiple > > files. > > The force option disables this function to enable scripts to work > > without modifying them. > > > > ``` > > diff --git a/src/rm.c b/src/rm.c > > index 354e2b0df..e4f9949f0 100644 > > --- a/src/rm.c > > +++ b/src/rm.c > > @@ -123,6 +123,16 @@ diagnose_leading_hyphen (int argc, char **argv) > > } > > } > > > > +static bool > > +find_duplicates (int n_files, char **files) > > +{ > > + for (int l =3D 0; l < n_files-1; l++) > > + for (int r =3D l+1; r < n_files; r++) > > + if (strcmp(files[l], files[r]) =3D=3D 0) > > + return true; > > + return false; > > +} > > + > > void > > usage (int status) > > { > > @@ -211,6 +221,7 @@ main (int argc, char **argv) > > bool preserve_root =3D true; > > struct rm_options x; > > bool prompt_once =3D false; > > + bool force_rm =3D false; > > int c; > > > > initialize_main (&argc, &argv); > > @@ -238,6 +249,7 @@ main (int argc, char **argv) > > x.interactive =3D RMI_NEVER; > > x.ignore_missing_files =3D true; > > prompt_once =3D false; > > + force_rm =3D true; > > break; > > > > case 'i': > > @@ -352,6 +364,17 @@ main (int argc, char **argv) > > uintmax_t n_files =3D argc - optind; > > char **file =3D argv + optind; > > > > + if (!force_rm && find_duplicates(n_files, file)) > > + { > > + /* Because usually when the input files are duplicated it means > > that the user > > + sumbitted both a directory and an * as separate arguments, > > probably by accident */ > > + fprintf (stderr, > > + "%s: input contains duplicates, most likely you've put = " > > + "both * and a file from the same directory.\n", > > + program_name); > > + return EXIT_FAILURE; > > + } > > + > > if (prompt_once && (x.recursive || 3 < n_files)) > > { > > fprintf (stderr, > > ``` > > An interesting proposal. > The main protection would be for `dir/ *` rather than `file_prefix_ *`. > The former would be unusual for a user to type, while the latter more usu= al, but wouldn't trigger the protection AFAICS. > This ads O(N^2) on each interaction, so if it was to be included probably= only enabled with --interactive. > > cheers, > P=C3=A1draig Yeah, true. Implemented it quickly and focused on the `dir/ *` scanerio at first, because that has happened to me -.- It was because I tried to do something quickly while editing command from backsearch and ended up with `rm tmp/ *` instead of `rm tmp/*`. To make the prefix_ protection it won't be possible without checking if a "prefix" exits on disk (eg. file and file1), which would be more time complex. I don't like the --interactive approach, as when you're trying to do something quickly, you probably won't opt for doing it interactively and you're probably either disabled or ignoring shell warnings. The more I think about this problem, the more I tilt towards implementing it in shell. If you'd've written "rm prefix_ *" and had 10k files there, it would be so much easier to detect it on the shell side than checking every filename with the other and triggering syscalls for every one... Either way if you have other solutions in mind, please update me, as it wouldn't require to configure shell in dockers or remote machines, thus making the solution more available. From unknown Sun Aug 17 10:19:17 2025 X-Loop: help-debbugs@gnu.org Subject: bug#60989: [PATCH] rm: fail on duplicate input if force not enabled Resent-From: "Philip Rowlands" Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Sun, 22 Jan 2023 18:20:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60989 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: patch To: =?UTF-8?Q?=C5=81ukasz?= Sroka , 60989@debbugs.gnu.org Received: via spool by 60989-submit@debbugs.gnu.org id=B60989.16744115444274 (code B ref 60989); Sun, 22 Jan 2023 18:20:01 +0000 Received: (at 60989) by debbugs.gnu.org; 22 Jan 2023 18:19:04 +0000 Received: from localhost ([127.0.0.1]:52616 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJevo-00016r-CY for submit@debbugs.gnu.org; Sun, 22 Jan 2023 13:19:04 -0500 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:45075) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJevj-00016J-OQ for 60989@debbugs.gnu.org; Sun, 22 Jan 2023 13:19:03 -0500 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 986EC5C0140; Sun, 22 Jan 2023 13:18:54 -0500 (EST) Received: from imap52 ([10.202.2.102]) by compute3.internal (MEProxy); Sun, 22 Jan 2023 13:18:54 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dimebar.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1674411534; x= 1674497934; bh=YYr12ZsJ9nnpzeCwEgetN4JFahZrerTtj4qYWaJB48I=; b=j L9RSvawV7JX4zaYMjt33DdXkc6OdqGhRyOgnA1myr9EW2I1bWzsgJhGlmKNcJYBj G0GoZ+SoIOg3jDiXU5p6t51zAA1NHcNapTt2EMOPXLXFBMiGnOI+TseILXiokfnu 9LimHnvCY/+y0c2Q3oxYQj0l2eS1Bpj/ZyxH1hY3X/nSJLxjrxlnMNF+Qb7e7PoL 6qzMhGCLWRcPTBe9SGn4M6lSLJS06snN7PuRR1xzP6cPF1YpO57lN9TdcjfmStjF H/8TMLIEnHQSbz3PRCJZ+f8BIYOTcF77gbkQ3GRnpwhoDfmKqvWvbNWArV81J3yT vhAEn9RBPNhHaiPrnaOMA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1674411534; x=1674497934; bh=Y Yr12ZsJ9nnpzeCwEgetN4JFahZrerTtj4qYWaJB48I=; b=a7xB65+RXNX1cmHgF 2kl//XmOaikOVNofkPQynJy3zIG8aZQRpNzfPGF3JSvI0MbOwpNOrEy0QOGOQl1D DoJEYhImjOz4k8NvDGYCo9XjIw32Ga63QZAwGjlmbd0Z1bgzsEE56HtSBUGjGxEy sD2MkUHpQ62oI1SwkIN3TAKGKgO+rWTds5eS56UHGK7OVCmbxCulSzDeL1YKYJnz snHb1bAXAWEmxMI3JAVAFk+mZEr/tsJhK6G9rbREtK5sRo1XIGo+JLk/m8ZUUHWw 75CBB7YXmzqK8YkKel7aQ5JOlYHaGiBdbSGglqETRuMOjo29HHxSsUjXguKfnZoP /9yIA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudduiedguddufecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtgfesthhqredtreerjeenucfhrhhomhepfdfr hhhilhhiphcutfhofihlrghnughsfdcuoehphhhrodgtohhrvghuthhilhhsseguihhmvg gsrghrrdgtohhmqeenucggtffrrghtthgvrhhnpeefudekleeiheefleegheehhfevkeff hfejudffgfeileelfeffudettefgffekudenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpehphhhrodgtohhrvghuthhilhhsseguihhmvggsrghr rdgtohhm X-ME-Proxy: Feedback-ID: i3ef94498:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 4A2DAC60091; Sun, 22 Jan 2023 13:18:54 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-85-gd6d859e0cf-fm-20230116.001-gd6d859e0 Mime-Version: 1.0 Message-Id: In-Reply-To: References: Date: Sun, 22 Jan 2023 18:18:04 +0000 From: "Philip Rowlands" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -0.7 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) On Sat, 21 Jan 2023, at 13:05, =C5=81ukasz Sroka wrote: > When the input files contain duplicates, then the rm fails. Because > duplicates occur most often when the * is used and the shell unwra= ps it. > There is a very common scenario when a user accidentally enters sp= ace > after a filename, or enters space instead of forward slash. To fail on duplicate FILE args, this bash function would do (lightly tes= ted, 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]=3D1 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 sugges= ts the file arguments are optional rm [option]=E2=80=A6 [file]=E2=80=A6 Cheers, Phil From unknown Sun Aug 17 10:19:17 2025 X-Loop: help-debbugs@gnu.org Subject: bug#60989: [PATCH] rm: fail on duplicate input if force not enabled Resent-From: =?UTF-8?Q?P=C3=A1draig?= Brady Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Sun, 22 Jan 2023 21:37:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60989 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: patch To: Philip Rowlands , =?UTF-8?Q?=C5=81ukasz?= Sroka , 60989@debbugs.gnu.org Received: via spool by 60989-submit@debbugs.gnu.org id=B60989.167442341829853 (code B ref 60989); Sun, 22 Jan 2023 21:37:02 +0000 Received: (at 60989) by debbugs.gnu.org; 22 Jan 2023 21:36:58 +0000 Received: from localhost ([127.0.0.1]:52730 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJi1K-0007lR-92 for submit@debbugs.gnu.org; Sun, 22 Jan 2023 16:36:58 -0500 Received: from mail-wm1-f44.google.com ([209.85.128.44]:37887) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pJi1I-0007lF-PC for 60989@debbugs.gnu.org; Sun, 22 Jan 2023 16:36:57 -0500 Received: by mail-wm1-f44.google.com with SMTP id bg13-20020a05600c3c8d00b003d9712b29d2so9365497wmb.2 for <60989@debbugs.gnu.org>; Sun, 22 Jan 2023 13:36:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :sender:from:to:cc:subject:date:message-id:reply-to; bh=7uRwgkCiNKMa8Sn5+TRGoTFB3d4z4asSk6RXlhiUBcc=; b=q2r0LECD09ew+0rmeIB+3tYSRNuZxgjVgFycVh7fHwLqqVG039zh5m/X+EDWXsNir0 A+W1dNlHFBFfkKDFz9RKY7FutN94H5AHsde5GRizbM32iwYtCHZYdwvxS+V9djez/EcM PKPe3dxLnXw6/R5sJ8cAkm9w7m6rA00sdzuiepQbD4d/R+P5SYVV1NeMLzBBeEQfQICh BvLNWiktQUUP3nqu8fLsKuB1PiquBvjxIRiFrujdIkC3L+VEjAdpkFWobtoqoWPCNFQx FeNP/kXyyllo5Pb61fb9rAj0JniJGC+OEBsr90MK0DEBNcEcTr9GxP5EsQZTTYvPlC+T i+yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :sender:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7uRwgkCiNKMa8Sn5+TRGoTFB3d4z4asSk6RXlhiUBcc=; b=eMIYo/0+BJSG2UEkQw/BlOhxx8vMZ7NnnC2P1uUM8b7NQOgfgAk4K5a8g7nUOAZ6pd gkI1OYKUSt1DjSK/rX5aycWKsus/o+FaeaA4EP9fAdsIbCcZJYF26Muz4YSYEZTXxmpj a8D2yXXl/sgAbxK1uFxJrUp9ZYa4wgXqBtqISUu4N9+fxt3cDQMd4LzaqDsIWAvjZ1iy aaz3IYemwhKswB/xwoXyGf3q2e6or2wvOhlog9iyfZfi3x3b4KRnjfo0+P4fzjdViaL1 5km+HwKdLEkyZTaY4zsdCV9/FEFvzjf+PRGSHw0MA+1A24InXpiEsRCztOKGaEwPyz0f j0oQ== X-Gm-Message-State: AFqh2kodpQ3kWtl8bM666icHtBtvJrlumoOIpip+nEhKrnGpk87rt0iP mL41As1lmZzQSqdBRyMTS/I= X-Google-Smtp-Source: AMrXdXv6ebAbgzlNxk3zDula+TwB8oXt24Y8nIMbCpQMUNJZ0sXRhBPTYOTPGPAxjuQMHs8dJY9Z7A== X-Received: by 2002:a05:600c:1c1a:b0:3da:fbd2:a324 with SMTP id j26-20020a05600c1c1a00b003dafbd2a324mr21825260wms.36.1674423410816; Sun, 22 Jan 2023 13:36:50 -0800 (PST) Received: from [192.168.1.9] (95-44-90-175-dynamic.agg2.lod.rsl-rtd.eircom.net. [95.44.90.175]) by smtp.googlemail.com with ESMTPSA id h11-20020a05600c314b00b003db2e3f2c7csm12352360wmo.0.2023.01.22.13.36.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 22 Jan 2023 13:36:50 -0800 (PST) Message-ID: Date: Sun, 22 Jan 2023 21:36:49 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Thunderbird/109.0 Content-Language: en-US References: From: =?UTF-8?Q?P=C3=A1draig?= Brady In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Score: -0.9 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.9 (-) 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