Linux 使用 Scp 时防止覆盖文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13410990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent overwriting of files when using Scp
提问by Desert Ice
I was copying some files using scp and i don't want to overwrite the already present files.
我正在使用 scp 复制一些文件,我不想覆盖已经存在的文件。
If i was using cp command, i think this can be done using cp -n.
如果我使用的是 cp 命令,我认为这可以使用 cp -n 来完成。
Is there a similar option for scp, i went through the documentation of SCP and there seems to be no such option.
scp 是否有类似的选项,我浏览了 SCP 的文档,似乎没有这样的选项。
Is rsync or sftp the way to go solve this problem?
rsync 或 sftp 是解决这个问题的方法吗?
Addition Info:
添加信息:
OS: Ubuntu 12.04
操作系统:Ubuntu 12.04
采纳答案by osulehria
rsync
seems to be the solution to your problem. Here's an example I got from here:
rsync
似乎是您问题的解决方案。这是我从这里得到的一个例子:
rsync -avz foo:src/bar /data/tmp
The -a option will preserve permissions, directory structure, ownership, and symlinks. You can also specify any of those options individually as well.
-a 选项将保留权限、目录结构、所有权和符号链接。您也可以单独指定任何这些选项。
-v and -z mean verbose and compress respectively. You don't really need them although -z is nice if you are copying large files.
-v 和 -z 分别表示详细和压缩。你并不真正需要它们,尽管 -z 如果你复制大文件很好。
回答by vladr
rsync over sshit will have to be.
回答by ManuelSchneid3r
I did not test it but maybe first mountung via sshfs
and then using cp
will do the trick.
我没有测试它,但也许首先通过安装sshfs
然后使用cp
可以解决问题。
回答by dsz
I've used rsync in the past for this, but found myself trying to grab from a windows box with CopSSH and no rsync :-( The following worked just fine for me, using file tests to eliminate the files that would be overwritten, and generating mutiple 'get' requests to an sftp instance.
我过去为此使用过 rsync,但发现自己试图从带有 CopSSH 的 Windows 框中抓取而没有 rsync :-( 以下对我来说效果很好,使用文件测试来消除将被覆盖的文件,并且向 sftp 实例生成多个“获取”请求。
( echo 'cd work/ftp/' ;
ssh <user>@<machine> 'cd work/ftp/ && ls -1 ITEM_SALE_SUMMARY_V.*.dat.xz' |
while read line; do [[ -f "$line" ]] || echo get "$line"; done
) | sftp <user>@<machine>
Just in case others need a non-rsync solution!
以防万一其他人需要非 rsync 解决方案!
回答by Bruno
I just found a simple hack. Mark the existing files as read-only.
我刚刚找到了一个简单的黑客。将现有文件标记为只读。
回答by Asif Khan
rsync -avz --ignore-existing /source folder/* user@remoteserver:/dstfolder/
--ignore-existing
will not overwrite the files on remote server or destination server*.
--ignore-existing
不会覆盖远程服务器或目标服务器上的文件*。
回答by SkyRaT
Just to supplement the other solutions:
只是为了补充其他解决方案:
For one ascii/bin file, you can do it with:
对于一个 ascii/bin 文件,您可以使用:
cat source_file | ssh host "test ! -f target_file && cat > target_file"
cat source_file | ssh host "test ! -f target_file && cat > target_file"