scp 或 cp -T 的 rsync 类似物(将目标视为文件)

scp 或 cp -T 的 rsync 类似物(将目标视为文件)

有了cp我可以:

$ mkdir q
$ touch s
$ cp -T s q  # I need same effect with scp and/or rsync.
cp: cannot overwrite directory 'q' with non-directory
$ ls q

如何使用scpand/or在一个命令中获得相同的效果rsync?即,当目标是目录时,我希望它以非零退出代码退出,而不需要实际的文件传输。我不想s将文件放在目录中q

当然,也可以单独检查。问题是如何用一个命令来做到这一点。特别是为了并发性。

答案1

如果有问题的远程目录存在,这将中止:

if ! ssh user@host "[[ -d /path/to/directory ]]"; then
    # do your scp or rsync
else
    echo "Remote location is a directory.  Aborting." 1&>2
    exit 1
fi

如果你想把它变成一句台词,那么:

ssh user@host "[[ -d /path/to/directory ]]" || <<scp or rsync>>

答案2

作为一个选项:

ssh remote-server 'cat > /tmp/asd' < /tmp/qwe

但不是参数。

例子:

$ ssh localhost 'cat > /tmp/oi' < /tmp/qwe ; echo $?
Authenticated to localhost ([::1]:22).
bash: /tmp/oi: Is a directory
Transferred: sent 2832, received 2548 bytes, in 0.0 seconds
Bytes per second: sent 60414.8, received 54356.2
1
$ ssh localhost 'cat > /tmp/asd' < /tmp/qwe ; echo $?                                                                                                                                                                     
Authenticated to localhost ([::1]:22).
Transferred: sent 2832, received 2496 bytes, in 0.0 seconds
Bytes per second: sent 62499.8, received 55084.5
0
$ ls -l /tmp/{qwe,asd,oi}                                                                                                                                                                                                 
-rw-r--r-- 1 george george    4 Apr 19 21:21 /tmp/asd
-rw-r--r-- 1 george george    4 Apr 19 21:19 /tmp/qwe

/tmp/oi:
total 0

相关内容