[Top][All Lists]

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

bug#61105: cp/mv: want a fatal --no-clobber

From: Pádraig Brady
Subject: bug#61105: cp/mv: want a fatal --no-clobber
Date: Fri, 27 Jan 2023 23:59:47 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Thunderbird/109.0

On 27/01/2023 20:52, Mike Frysinger wrote:
i've been under the mistaken assumption that the -n/--no-clobber option exits
non-zero when the target exists, but someone pointed out to me recently that
it silently ignores existing files.  can we get a setting to control this ?

Yes --no-clobber={skip (default), fail} would have some value.
Especially for handling multiple files.

basically i've been writing things like:
if ! cp -n foo bar; then
   ... error out because bar already exists, or otherwise failed ...

when really i need to write:
if [ -e bar ] || ! cp foo bar; then
   ... error out ...

The above is racy if the file is created between the test and the cp.

I suppose you could leverage the shell to create the file atomically like:

cp-n-fail() {
  (set -C && cat < "$1" > "$2")

cp-a-n-fail() {
  cp-n-fail "$1" "$2" &&
  cp -a --attributes-only "$1" "$2"

But yes hack, only handles single files, doesn't handle sparseness, ...


reply via email to

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