cp options --no-clobber vs. --update
It's meaningless to combine -n
and -u
.
Use -n
if you never want to overwrite an existing file.
Use -u
if you don't want to overwrite newer files.
The case where the two differ, then, is where you have a destination file that's older than the source file. Consider what you want to happen for this case, and write your command accordingly.
I'd expect that -n
is more efficient than -u
- but the effect is unlikely to be at all measurable.
(In the above, 'older', 'newer' etc. are all in terms of the mtime of the files.
Related videos on Youtube
MattBianco
Updated on September 18, 2022Comments
-
MattBianco almost 2 years
I want to copy a single file if (and only if) the destination does not exist. The source file changes rarely, maybe once a month. The destination almost never exists.
Are there any differences between the
-n
and-u
options? (Or both!)cp
is being called directly fromcrond
. No othercp
options are used.The same cron job is called on several machines at the same time, reading from the same source and writing to the same destination (both on a shared GFS global file system). The destination file will be moved shortly thereafter by another process, so the only time it could exist is during the race when the cron job executes simultaneously on several nodes.
Which would be more efficient? :
cp -n source dest
cp -u source dest
cp -nu source dest
cp -pu source dest
I'm currently leaning towards the simple
-n
alternative.-
Stéphane Chazelas about 9 yearsHave you checked the manual? (
info cp
) -
MattBianco about 9 yearsYes,
info coreutils "cp invocation"
mostly mentions how-u
works with-p
. No recommendations or no mentioning of the combo with both-u
and-n
. -
FloHimself about 9 yearsYou should mention if the source file could change / gets updated or is always the same file.
-
MattBianco about 9 yearsThank you @FloHimself , very relevant information. Question updated.
-
MattBianco about 9 yearsActually, I ended up using
-pu
, because my coreutils was too old, and didn't have the-n
option. :-(