How to skip existing files when copying with scp

Last updated on August 20, 2020 by Dan Nanni

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.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean