cp options --no-clobber vs. --update

5,481

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.

Share:
5,481

Related videos on Youtube

MattBianco
Author by

MattBianco

Updated on September 18, 2022

Comments

  • MattBianco
    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 from crond. 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
      Stéphane Chazelas about 9 years
      Have you checked the manual? (info cp)
    • MattBianco
      MattBianco about 9 years
      Yes, 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
      FloHimself about 9 years
      You should mention if the source file could change / gets updated or is always the same file.
    • MattBianco
      MattBianco about 9 years
      Thank you @FloHimself , very relevant information. Question updated.
    • MattBianco
      MattBianco about 9 years
      Actually, I ended up using -pu, because my coreutils was too old, and didn't have the -n option. :-(