Celeb Glow
general | March 07, 2026

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

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

4 Answers

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:

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

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.

2

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy