Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 14 down vote accepted

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

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

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

Use rsync

rsync --progress -r -u /from/one/* /to/another/directory
share|improve this answer

Your Answer


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.