No clobber scp - scp without overwriting

I don't allow ftp to my website. The only access I allow is ssh, which means I need to scp files and images that I want to add there.

I have a problem with images particularly because I could accidentally over-write an older image with a newer image of the same name. While that may be exactly what I want to do, the new image might not be right for older web pages that reference that name.

I have made that mistake already. I can fix it from backups, but in that particular case I didn't notice the error for several years - it was not until someone pointed it out in a comment that I realized my mistake. By then, I had no backups old enough and I couldn't easily recreate the image because it was showing something on old software that I could no longer run. All I could do was apologize!

I don't want that to happen again.

No Clobber in SSH

I thought there surely must be a "no clobber" or "no over-write" or "no update" option in ssh. There is not. Apparently the developers see no need of such a thing and will always suggest using rsync instead because rsync has a flag for that:

--ignore-existing       skip updating files that exist on receiver

That DOES work, but it's not helpful for my need. When I'm preparing an article, I might have half a dozen or more files with similar names. When I'm ready to scp them up, I reference them with a wildcard:

scp control_* ... # WILL over-write files
rsync control_* -v -e ssh  --ignore-existing ... # WON'T over-write

The problem with that is that it ignores silently. We can increase the verbosity:

rsync control_* -vv -e ssh  --ignore-existing ... # WON'T over-write

But that's too noisy for my tastes:

existing . stage 
delta-transmission enabled
control_additional_domains.jpg exists
control_additional_domains_lg.jpg exists
control_domain.jpg exists
control_domain_lg.jpg exists
total: matches=0  hash_hits=0  false_alarms=0 data=53802

sent 53994 bytes  received 42 bytes  21614.40 bytes/sec
total size is 282872  speedup is 5.23

Other ways to avoid over-writing with ssh

There are plenty of ways around this. You could scp stuff up to a directory and then use ssh to call a server side script that tests what exists before copying to the final destination, but I wanted something I could use without depending on a server side script.

This is what I finally decided to use. I wrote it in Perl, but you could do something similar in Bash or indeed any language. This script does do (or at least attempt) two scp's, but that isn't as inefficient as it sounds: if the first scp succeeds, it won't do the second, so really there's only a very little extra overhead.

$dest=pop @ARGV;
print "$dest\n";
foreach (@ARGV) {
system("scp $dest/$_ /tmp > /dev/null 2>&1");
if ( -e "/tmp/$_") {
  push @errors, $_;
system("scp $_ $dest");
foreach (@errors) {
  print "Could not copy $_ -  exists!\n";

That's it. You use it much as you would ssh:

myscp foo* [email protected]:folder/put_them_here/
control_additional_domains.jpg                100%   28KB  27.8KB/s   00:00    
control_additional_domains_lg.jpg             100%   89KB  89.4KB/s   00:00    
fobbydo                                       100%   29     0.0KB/s   00:00    
Could not copy control_domain.jpg -  exists!
Could not copy control_domain_lg.jpg -  exists!
Could not copy control_local.jpg -  exists!

By showing existing files at the end, I'm not going to miss seeing them. You could also write @errors to a file so that you could easily examine the problems.

Got something to add? Send me email.

(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Use scp without overwriting existing files

1 comment

Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Anthony Lawrence

Sun Oct 7 20:44:29 2012: 11369   TonyLawrence


Also: don't use /tmp on a multiuser machine. You may want to set a specific directory just for this purpose.

Kerio Samepage

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

One day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" (Larry Wall)

This post tagged: