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.

link|improve this question
feedback

2 Answers

up vote 0 down vote accepted

Using the update option (-u) with cp should do it for you.

http://beginnerlinuxtutorial.com/help-tutorial/basic-linux-commands/cp-linux-copy-command/

link|improve this answer
thanks, it works for me – Eli Sep 29 '11 at 15:37
Don't forget that voting and accepting answers (once you find the most useful) is what drives the site. – Dennis Sep 29 '11 at 15:50
feedback

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
link|improve this answer
Isn't bash most common? – Rob Sep 29 '11 at 16:58
@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 Sep 29 '11 at 23:12
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.