39
5
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.
39
5
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.
57
Using the update option (-u) with cp should do it for you.
http://beginnerlinuxtutorial.com/help-tutorial/basic-linux-commands/cp-linux-copy-command/
20
Use rsync
rsync --progress -r -u /from/one/* /to/another/directory
3Just for completeness: -r
means recursive operation into subdirectories and -u
to keep newer files at destination (=update). --progress
shows progress information during operation. – Jörg Gottschlich – 2017-01-01T16:59:35.913
6
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
Isn't bash most common? – Rob – 2011-09-29T16:58:52.727
1@Rob, yes. This works in bash too, obviously. I just didn't have bash (or any standard Linux box) at hand when I wrote it. – Kusalananda – 2011-09-29T23:12:01.820
4
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.
1
This is kind of cryptic. Can you expand your answer to be a little more explicit, maybe include an example? Thanks. from Review
– fixer1234 – 2016-11-08T18:03:56.093yes -> Answer yes to all the questions. – Felippe Silvestre – 2016-11-08T18:34:35.300
21To save future readers some time: this question is tagged Linux. Non-GNU cp implementations (BSD, macOS, etc.) lack the
-u
option. You can usersync --update
instead. – user31389 – 2016-10-21T13:04:20.347