45

I was copying some files using scp and i don't want to overwrite the already present files.

If i was using cp command, i think this can be done using cp -n.

Is there a similar option for scp, i went through the documentation of SCP and there seems to be no such option.

Is rsync or sftp the way to go solve this problem?

Addition Info:

OS: Ubuntu 12.04

Desert Ice
  • 4,461
  • 5
  • 31
  • 58
  • [scp-without-replacing-existing-files-in-the-destination](https://unix.stackexchange.com/questions/14191/scp-without-replacing-existing-files-in-the-destination), [how-to-use-rsync-to-sync-local-and-remote-directories](https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories), [copy from remote gist](https://gist.github.com/ppdouble/273dcfd7409dab2fb7739d4def6598fa) – Nick Dong Jun 12 '23 at 00:27

7 Answers7

40

rsync seems to be the solution to your problem. Here's an example I got from here:

rsync -avz foo:src/bar /data/tmp

The -a option will preserve permissions, directory structure, ownership, and symlinks. You can also specify any of those options individually as well.

-v and -z mean verbose and compress respectively. You don't really need them although -z is nice if you are copying large files.

yprez
  • 14,854
  • 11
  • 55
  • 70
osulehria
  • 711
  • 5
  • 7
19

I just found a simple hack. Mark the existing files as read-only.

Bruno
  • 191
  • 1
  • 2
  • 1
    That makes perfect sense if you don't want to overwrite existing files: `chmod u-w srcfile; scp -p srcfile $USER@$host:$directory/`. From the scp man page: `-p Preserves modification times, access times, and modes from the original file.` – Ikar Pohorský Jun 09 '15 at 08:17
15
rsync -avz --ignore-existing /source folder/* user@remoteserver:/dstfolder/

--ignore-existing will not overwrite the files on remote server or destination server*.

duplode
  • 33,731
  • 7
  • 79
  • 150
Asif Khan
  • 151
  • 1
  • 3
0

I did not test it but maybe first mountung via sshfs and then using cp will do the trick.

hakre
  • 193,403
  • 52
  • 435
  • 836
ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103
0

I've used rsync in the past for this, but found myself trying to grab from a windows box with CopSSH and no rsync :-( The following worked just fine for me, using file tests to eliminate the files that would be overwritten, and generating mutiple 'get' requests to an sftp instance.

(  echo  'cd work/ftp/' ; 
   ssh <user>@<machine> 'cd work/ftp/ && ls -1 ITEM_SALE_SUMMARY_V.*.dat.xz' |
   while read line; do [[ -f "$line" ]] || echo get "$line"; done 
) | sftp  <user>@<machine>

Just in case others need a non-rsync solution!

dsz
  • 4,542
  • 39
  • 35
0

Just to supplement the other solutions:

For one ascii/bin file, you can do it with:

cat source_file | ssh host "test ! -f target_file && cat > target_file"

SkyRaT
  • 285
  • 3
  • 6
-3

rsync over ssh it will have to be.

vladr
  • 65,483
  • 18
  • 129
  • 130