# # Use scp without overwriting existing files
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

No clobber scp - scp without overwriting

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© October 2012 Anthony Lawrence

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
control_local.jpg
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.

#!/usr/bin/perl
$dest=pop @ARGV;
print "$dest\n";
foreach (@ARGV) {
unlink("/tmp/$_");
system("scp $dest/$_ /tmp > /dev/null 2>&1");
if ( -e "/tmp/$_") {
  push @errors, $_;
  next;
}
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.


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





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

Printer Friendly Version

->
-> Use scp without overwriting existing files

1 comment


Inexpensive and informative Apple related e-books:

Digital Sharing Crash Course

Take Control of Preview

Take Control of the Mac Command Line with Terminal, Second Edition

Take Control of Parallels Desktop 12

Take control of Apple TV, Second Edition





More Articles by © Anthony Lawrence







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

gravatar


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



------------------------


Printer Friendly Version

Have you tried Searching this site?

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


Printer Friendly Version





We are standing on a whale fishing for minnows. (Joseph Campbell)




Linux posts

Troubleshooting posts


This post tagged:

Basics

Linux

Scripting

Shell



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode