Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emulate 'cp --update' behavior on Mac OS X?

Tags:

shell

macos

The GNU/Linux version of cp has a nice --update flag:

-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing

The Mac OS X version of cp lacks this flag.

What is the best way to get the behavior of cp --update by using built-in system command line programs? I want to avoid installing any extra tools (including the GNU version of cp).

like image 766
amrox Avatar asked Jul 15 '09 16:07

amrox


People also ask

How do I use cp on Mac?

Copy a file or folder locally In the Terminal app on your Mac, use the cp command to make a copy of a file. The -R flag causes cp to copy the folder and its contents. Note that the folder name does not end with a slash, which would change how cp copies the folder.

What happens if the destination file specified in cp command does not exist?

2. What happens if the destination file specified in cp command does not exist? Explanation: If the destination file does not exist, then cp command will automatically create a file with the same name and then it copies the contents of the source file to the file which is created.


2 Answers

rsync has an -u/--update option that works just like GNU cp:

$ rsync -u src dest
like image 70
Noah Medling Avatar answered Nov 16 '22 01:11

Noah Medling


Also look at rsync's other options, which are probably what you actually want:

    -l, --links                 copy symlinks as symlinks
    -H, --hard-links            preserve hard links
    -p, --perms                 preserve permissions
        --executability         preserve executability
    -o, --owner                 preserve owner (super-user only)
    -g, --group                 preserve group
        --devices               preserve device files (super-user only)
        --specials              preserve special files
    -D                          same as --devices --specials
    -t, --times                 preserve times

    -a, --archive
          This is equivalent to -rlptgoD. It is a quick way of saying you want recursion 
          and want to preserve almost everything (with -H being a  notable  omission).   The
          only exception to the above equivalence is when --files-from is specified, in which 
          case -r  is not implied.

          Note that -a does not preserve hardlinks, because finding multiply-linked files is
          expensive.  You must separately specify -H.
like image 26
Eddie Caplan Avatar answered Nov 16 '22 02:11

Eddie Caplan