How can I get the scp command to overwrite the destination folder

44

7

I am using the scp command to copy some files to a remote pc, as you do with scp :)

I note that the default behaviour of an scp copy for files is to overwrite any existing files. Now I want to copy a folder so I do basically the same thing:

scp -r <source_path> user@myOtherPc:<dest_path>

Where the parts in <> are my folder paths. However when I run this I get the message "file exists". Is there a way around this? some sort of force over-write?

Thanks, Fodder

code_fodder

Posted 2013-09-23T14:15:50.193

Reputation: 1 057

Warning: If you use the root user the file will have the root ownership. Not at all a secure idea if at the end there will be some file with read/write/execute attributes set for more than the owner... – Hastur – 2014-07-02T10:45:55.447

2Could you give some examples of path ? I do not perform to reproduce this on my system. Also, have you checked file permissions ? – Levans – 2013-09-23T15:51:07.990

Does it give you any useful info if you use verbose mode? -v. Also, are you using absolute or relative path on the destination side? I think if the destination folder already exists, it is going to create the source path inside the destination folder rather than overwrite (testdir/testdir) – beroe – 2013-09-24T01:47:12.700

2

It is strange that you get this. Supposedly scp doesn't have noclobber...

– beroe – 2013-09-24T01:57:52.727

1@Levans I did not check the permissions, I have just taken a look and I think you may be right. I deleted everything in the target area and started again using only the "scp" command and using "root" user and I was able to copy the files/folder multiple times :) ... I am not 100% sure what permission issue I had, but now I have a "handle" on the issue I can probably figure it, thanks – code_fodder – 2013-09-24T06:38:39.213

1@beroe Yes, this is what I thought too... it did confuse me, but I think I have made some sort of user error here with the permissions, I am not linux expert yet :( But I will try the verbose-ness next time I have an issue like that to get more info, thanks. Also I have noticed that sometimes you get the "folder-inside-the folder" happening... but that is another issue :o – code_fodder – 2013-09-24T06:41:51.960

Answers

30

As said before, scp happily overwrites any file that is already present.

The "file exists" issue can only occur when you have some other process (like a concurrent scp process, or something else) writing folders and files to the same destination. Consider using rsync instead.

nicolasochem

Posted 2013-09-23T14:15:50.193

Reputation: 401

1I would only add: ...as long as you have write access to that file... – SDsolar – 2017-10-31T22:38:45.827

but for folders, does it merge the folder or replace it? – Dr_Hope – 2019-08-29T23:20:18.657

5

Like Levans, I have been unable to replicate this, but have you considered using rsync over ssh instead? If you're copying large numbers of files, rsync may be a better option than scp. There are a number of good guides to it online, such as these:

http://troy.jdmz.net/rsync/index.html https://calomel.org/rsync_tips.html

That first link deals with automated backups via cron, so some of the instructions (like creating an ssh key without a passphrase) may not be relevant to you.

Ben

Posted 2013-09-23T14:15:50.193

Reputation: 1 258

1This can work, but is a tad like using a sledgehammer to swat a fly. ;-) – SDsolar – 2017-10-31T22:38:03.300

Interesting, I did not know you can use rsync over in that way... just had a quick look at the man page and looks like you can use it in a very similar way to scp with <source_path> and <user@host:dest_path> parameters. I am not 100% sure that is what I want since I want to always copy/overwrite without caring the status of what is in the dest, but still a nice idea :) – code_fodder – 2013-09-24T06:45:24.273

Yeah, it might be worth having a play because it should copy over the file if it has been changed (or the part of the file that has been changed, I'd need to double-check and I'm a little too tired right now). – Ben – 2013-09-24T12:11:03.263

haha.... don't worry about checking it, I can do that stuff... but thanks for the info :) – code_fodder – 2013-09-24T12:35:16.407

Ah, cool, I can stop looking at matchsticks as a means to prop my eyelids open. ;) – Ben – 2013-09-24T12:45:25.303

3

You will receive this error message if the destination directory already contains a file with the same name as the source directory you are attempting to transfer. You can not have a file with the same name as a directory in the same directory.

EvR2f

Posted 2013-09-23T14:15:50.193

Reputation: 31