63

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

7
  • 2
    Could you give some examples of path ? I do not perform to reproduce this on my system. Also, have you checked file permissions ?
    – Levans
    Sep 23, 2013 at 15:51
  • 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
    Sep 24, 2013 at 1:47
  • 2
    It is strange that you get this. Supposedly scp doesn't have noclobber...
    – beroe
    Sep 24, 2013 at 1:57
  • 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 Sep 24, 2013 at 6:38
  • 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 Sep 24, 2013 at 6:41

4 Answers 4

7

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.

7
  • 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 :) Sep 24, 2013 at 6:45
  • 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
    Sep 24, 2013 at 12:11
  • haha.... don't worry about checking it, I can do that stuff... but thanks for the info :) Sep 24, 2013 at 12:35
  • 1
    This can work, but is a tad like using a sledgehammer to swat a fly. ;-)
    – SDsolar
    Oct 31, 2017 at 22:38
  • 1
    @Ben I seem to have forgotten to tick that answer waaay back when! - updated sorry! Apr 4, 2020 at 16:23
50

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.

3
  • 3
    I would only add: ...as long as you have write access to that file...
    – SDsolar
    Oct 31, 2017 at 22:38
  • 1
    but for folders, does it merge the folder or replace it?
    – Code42
    Aug 29, 2019 at 23:20
  • Thanks for pointing out possible interferences from running processes
    – kaiya
    Jul 13, 2022 at 12:25
8

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.

0
0

this rsync command will do it:

    export ssh_server=csitea.net # the domain name or ip if you want 
    export ssh_user=ubuntu # the ssh user to use to
    export src_dir=/home/ubuntu/opt/ # the source dir
    # the full path to the ssh identity file 
    export ssh_idty=~/.ssh/id_rsa.aws-ec2.csitea.net 
    export tgt_dir=/home/me/opt/ # the target dir
    # Action !!!
    rsync -e "ssh -l USERID -i $ssh_idty" -av -r --partial --progress --human-readable \
        --stats $ssh_user@$ssh_server:$src_dir $tgt_dir
    # and check the result
    clear ; find $tgt_dir -type d -maxdepth 1|sort -nr| less

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .