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:
Similarly, to upload all local files (over SSH) without overwriting any duplicate remote files:
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:
$ 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.
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.
Did you find this tutorial helpful? Then please be generous and support Xmodulo!