How to skip existing files when copying with scp

Question: I want to download (or upload) files from (or to) a remote server using the scp command. In this case, I want to skip existing files, so that they will not get overwritten by scp. But the scp command would blindly overwrite existing files if the same name files exist at either host. How can I copy files over without overwriting existing files, so that only new files are downloaded (or uploaded) by scp?

Suppose you have a list of files on a remote host, some of which already exist locally. What you want is to transfer only those files that are not found locally. If you blindly run scp with wildcard, it would fetch all remote files (existing as well as non-existing files), and overwrite existing local files. You want to avoid this.

In another similar situation, you may want to upload local files to a remote site, but without replacing any remote files.

Here are a few ways to skip existing files when transferring files with scp.

Method One: rsync

If the local and remote hosts have rsync installed, using rsync will be the easiest way to copy only new files over, since rsync is designed for incremental/differential backups.

In this case, you need to explicitly tell rsync to "skip" any existing files during sync. Otherwise, rsync will try to use file modification time to sync two hosts, which is not what you want.

To download all remote files (over SSH) while skipping existing local files:

$ rsync -av --ignore-existing user@remote_host:/path/to/remote/directory/* /path/to/local/directory/

Similarly, to upload all local files (over SSH) without overwriting any duplicate remote files:

$ rsync -av --ignore-existing /path/to/local/directory/* user@remote_host:/path/to/remote/directory/

Method Two: getfacl/setfacl

Another way to scp only new files over to a destination is by leveraging file permissions. More specifically, what you can do is to make all destination files "read-only" before scp transfer. This will prevent any existing destination files from being overwritten by scp. After scp transfer is completed, restore the file permissions to the original state. The ACL command-line tools (getfacl and setfacl) come in handy when you temporarily change file permissions and restore them.

Here is how to scp files without replacing existing files using ACL tools.

To download all remote files (over SSH) while skiping existing local files:

$ cd /path/to/local/directory
$ getfacl -R . > permissions.txt
$ chmod -R a-w .
$ scp -r user@remote_host:/path/to/remote/directory/* .
$ setfacl --restore=permissions.txt

Similarly, to upload all local files without replacing any remote file, first back up the file permissions of the remote destination folder. Then remove write-permission from all files in the remote destination folder. Finally, upload all local files, and then restore the saved file permissions.

Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

One thought on “How to skip existing files when copying with scp

  1. Nice trick, but if you want to transfer with the SSH security, I suggest to use mc command. It lets you connect with ssh protocol, and when you copy files from local to remote server, it will ask you if you want to skip files with the same name or replace it.

Leave a comment

Your email address will not be published. Required fields are marked *