How do I copy a file in Linux only when the file being copied is newer than the version at the destination?
If the file at the destination is newer, I want the file copy to not go ahead.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityUsing the update option (-u) with cp should do it for you.
http://beginnerlinuxtutorial.com/help-tutorial/basic-linux-commands/cp-linux-copy-command/
-u
option. You can use rsync --update
instead.
Oct 21, 2016 at 13:04
coreutils
package with pkg install coreutils
. Then, among others, you will have GNU cp under the name gcp
. Note that there is also brew package coreutils
which also provide gcp
.
Use rsync
rsync --progress -r -u /from/one/* /to/another/directory
-r
means recursive operation into subdirectories and -u
to keep newer files at destination (=update). --progress
shows progress information during operation.
-t
option in order to keep the timestamp from the source
You're not saying what shell you're using, so I'm going to assume ksh
:
if [[ file1 -nt file2 ]]; then cp file1 file2; fi
yes|cp -ruv /from/* /to/.
yes - Answer yes to all the questions.
r - Recursive
u - update
v - Progress
works like xargs.
I don't know how to explain academically.