scp:不要覆盖Linux或者Unix系统上的现有文件
时间:2020-01-09 10:46:03 来源:igfitidea点击:
我使用scp将文件上传到网络存储提供的CDN公司。
我不想覆盖现有文件。
在Linux,Mac OS X或者Unix等操作系统上使用scp命令时,如何防止文件覆盖?
简短答案不可能。
长答案
如果所有者拥有写权限,scp
命令将始终覆盖已经存在的文件。
我建议您使用rsync
命令。
语法是
rsync --ignore-existing file1 user@server1:/dest/ rsync --ignore-existing file.to.upload [email protected]:/path/to/dest/ ### add ssh, just in case ;) ### rsync -e ssh --ignore-existing file.to.upload [email protected]:/path/to/dest/ rsync -avP --ignore-existing *.png [email protected]:/stoage/static/c/theitroad.local/images/ rsync --ignore-existing file1 /path/to/dest/
" --ignore-existing"选项会跳过更新接收器上存在的文件,例如server1.theitroad.local。
Shell脚本示例
以下脚本将使用rsync命令将文件从您的计算机上载到远程服务器。
首先,请确保您设置了公共密钥身份验证:
#!/bin/bash # Purpose: Batch file uploader from my desktop to the remote server # Author: theitroad <www.theitroad.local> under GPL v2.x+ # ---------------------------------------------------------------- _server="server1.theitroad.local" _user="theitroad" ## static url mapping ## _cdnbase="http://static.theitroad.org/images" _dest="/var/www/images/" _cdnurls="" _isuploaded=0 _files="$@" ## rsync args ## _args="-aPv --ignore-existing" ## fail safe ## [[ "$_files" == "" ]] && { echo "Usage:./script foo.png ./script foo.png bar.png ./script *.mov ./script test.giffile1.png file2.png"; exit 0; } ## Let us start uploading files. Bash for loop allows us to do a few more additional things, ## but I am keeping it simple here for demo purpose for i in $_files do # make sure $i file exist # if [ -f "$i" ] then rsync "$_args" "$i" ${_user}@${_server}:${_dest} _cdnurls="${_cdnurls}\n${_cdnbase}/${i##*/}" _isuploaded=1 else echo "Error $i file not found." fi done if [ $_isuploaded -eq 1 ] then echo "CDN urls:" echo -e "$_cdnurls" echo fi
您可以如下使用脚本:
sending incremental file list test-image.gif 950 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 1053 bytes received 31 bytes 197.09 bytes/sec total size is 950 speedup is 0.88 CDN urls: http://static.theitroad.org/images/test-image.gif
输出示例:
./script test.gif
再次运行:
sending incremental file list sent 60 bytes received 12 bytes 13.09 bytes/sec total size is 950 speedup is 13.19 CDN urls: http://static.theitroad.org/images/test-image.gif
输出示例:
##代码##