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
).
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.
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.
rsync
has an -u
/--update
option that works just like GNU cp
:
$ rsync -u src dest
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With