Sftp – how to only copy files from folder that don’t exist in destination folder

sftp

I am wondering if it's possible to get files with sftp, but prevent it from re-downloading files that already exist in the destination folder?

Best Answer

sftp has limited capabilities. Nonetheless, the get command has an option which may do the trick: get -a completes partial downloads, so if a file is already present on the client and is at least as large as the file on the server, it won't be downloaded. If the file is present but shorter, the end of the file will be transferred, which makes sense if the local file is the product of an interrupted download.

The easiest way to do complex things over SFTP is to use SSHFS. SSHFS is a filesystem that uses SFTP to make a remote filesystem appear as a local filessytem. On the client, SSHFS requires FUSE, which is available on most modern unices. On the server, SSHFS requires SFTP; if the server allows SFTP then you can use SSHFS with it.

mkdir server
sshfs server.example.com:/ server
rsync -a server/remote/path /local/path/
fusermount -u server

Note that rsync over SSHFS can't take advantage of the delta transfer algorithm, because it's unable to compute partial checksums on the remote side. That's irrelevant for a one-time download but wasteful if you're synchronizing files that have been modified. For efficient synchronization of modified files, use rsync -a server:/remote/path /local/path/, but this requires SSH shell access, not just SFTP access. The shell access can be restricted to the rsync command though.

Related Question