Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent overwriting of files when using Scp [closed]

Tags:

linux

scp

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

like image 674
Desert Ice Avatar asked Nov 16 '12 05:11

Desert Ice


People also ask

How do I use scp without overwriting existing files?

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.

Does scp overwrite existing files?

By default, existing files are overwritten. To control overwrite behavior, use --overwrite. (If the files are identical no transfer occurs regardless of this setting value.) Because scp uses authentication and encryption provided by ssh, a Secure Shell server must be running on the remote computer.

What is difference between scp and rsync?

Copying files and directories with SCP or Rsync Secure Copy (SCP) uses SSH to copy only the files or directories that you select. On first use, Rsync copies all files and directories and then it copies only the files and directories that you have changed. It does not copy all the files and directories again.

How do I scp all contents of a directory?

To copy a directory (and all the files it contains), use scp with the -r option. This tells scp to recursively copy the source directory and its contents. You'll be prompted for your password on the source system ( deathstar.com ). The command won't work unless you enter the correct password.


2 Answers

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.

like image 81
osulehria Avatar answered Oct 05 '22 23:10

osulehria


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

like image 23
Bruno Avatar answered Oct 06 '22 00:10

Bruno