Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 2 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/

share|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
add comment

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
share|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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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