Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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 an entire directory into a directory of the same name without replacing the content in the destination directory? (instead, I would like to add to the contents of the destination folder)

share|improve this question

migrated from stackoverflow.com Jun 2 '11 at 0:50

This question came from our site for professional and enthusiast programmers.

Use rsync, and pass -u if you want to only update files that are newer in the original directory, or --ignore-existing to skip all files that already exist in the destination.

rsync -au /local/directory/ host:/remote/directory/
rsync -a --ignore-existing /local/directory/ host:/remote/directory/

(Note the / on the source side: without it rsync would create /remote/directory/directory.)

share|improve this answer
@Anthon I don't understand your comment and I don't see an answer or comment by chandra. --ignore-existing does add without replacing, what data loss do you see? – Gilles Nov 27 '13 at 9:59
Sorry, I only looked at your first example that is where you can have data loss (and is IMHO not what the OP asked for), if you include --ignore-existing data-loss should not happen. – Anthon Nov 27 '13 at 10:08

scp will overwrite the files if you have write permissions to them.

In other words: You can make scp effectively skip said files by temporarily removing the write permissions on them (if you are the files' owner, that is).

share|improve this answer
Thanks for this. Was exactly the trick I was looking for. – saccharine Jul 16 '13 at 21:02
make sure you copy the files back you add a * to do so. Example scp -r user@server.com:/location/of/files/* /local/location/ – Rick May 27 '15 at 19:16

You can copy only new files by date. Use find

scp  `find /data/*.gz -type f -mtime +7` USER@SERVER:/backup/
share|improve this answer

If you can make the destination file contents read-only:

find . -type f -exec chmod a-w

before running scp (it will complain and skip the existing files). And change them back afterward ( chmod +w to get umask based value). If the files do not all have write permission according to your umask, you would somehow have to store the permissions so that you can restore them.

(Gilles' answer overwrites existing files if locally they are newer, I lost valuable data that way. Do not understand why that wrong and harmful answer has so many up votes).

share|improve this answer
I don't get it: how did rsync --ignore-existing cause you to lose data? – Gilles Nov 27 '13 at 10:01
I got the error find: missing argument to '-exec' using this command, and instead had to use: find . -type f -exec chmod a-w {} \;. My linux is bad, ymmv. – wpearse Apr 6 '15 at 0:16

I had a similar task, in my case I could not use rsync, csync, or FUSE because my storage has only SFTP. rsync could not change the date and time for the file, some other utilities (like csync) showed me other errors: "Unable to create temporary file Clock skew detected".

If you have access to the storage-server - just install openssh-server or launch rsync as a daemon here.

In my case - I could not do this and the solution was: lftp. lftp's usage for syncronization is below:

lftp -c "open -u login,password sftp://sft.domain.tld/; \
    mirror -c --verbose=9 -e -R -L /srs/folder /rem/folder"

/src/folder - is the folder on my PC, /rem/folder - is sftp://sft.domain.tld/rem/folder.

You may find man pages by the link: http://lftp.yar.ru/lftp-man.html

share|improve this answer

scp does overwrite files and there's no switch to stop it doing that, but you can copy things out the way, do the scp and then copy the existing files back. Examples:

  1. Copy all existing files out the way

    mkdir original_files ; cp -r * original_files/
  2. Copy everything using scp

    scp -r user@server:dir/* ./
  3. Copy the original files over anything scp has written over:

    cp -r original_files/* ./
share|improve this answer

Your Answer


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