将文件夹scp到远程系统,保持目录布局 [英] scp a folder to a remote system keeping the directory layout
问题描述
我有一个带有数百个嵌套子文件夹的大型目录树.我只需要将4个文件夹及其内容复制到远程系统,但是我需要将目标文件夹的结构保持不变.
I have a large directory tree with hundreds of nested sub-folders. I need to copy only 4 folders and their contents to a remote system, but I need to destination folder structure to be kept the same.
E.G.
./test/sub1/subsub1/hello.txt
./test/sub1/subsub2/hello2.txt
./test/sub2/hello3.txt
我想将./test/sub1/subsub1/*复制到诸如user @ system:〜/test/sub1/subsub1/*这样的目标,但是我不想复制subsub2或sub2.
I want to copy ./test/sub1/subsub1/* to a target such as user@system:~/test/sub1/subsub1/* but I do not want to copy subsub2 or sub2.
我尝试如下使用scp:
I have tried using scp as follows:
scp -r ./test/sub1/subsub1 me@my-system:~/test/sub1/subsub1
结果:scp:/test/sub1/subsub1:没有这样的文件或目录
The result: scp: /test/sub1/subsub1: No such file or directory
我也尝试过:
scp -r ./test/sub1/subsub1 me@my-system:~/test
这可行,但是将所有文件转储到一个目录中. /test/sub1/subsub1目录结构未维护.
This works, but dumps all the files into a single directory. The /test/sub1/subsub1 directory structure is not maintained.
如何在保持文件夹结构的同时复制文件夹?
How can I copy a folder, whilst maintaining its structure?
推荐答案
您需要两次通过解决方案.首先,确保目标目录在远程主机上存在:
You need a two-pass solution. First, ensure the target directory exists on the remote host:
ssh me@my-system 'mkdir -p ~/test/sub1/subsub1'
然后,您可以复制文件.我建议使用rsync
而不是scp
,因为它是为同步目录而设计的.用法示例:
Then, you can copy your files. I recommend using rsync
instead of scp
, since it's designed for syncing directories. Example usage:
rsync -r -e ssh ./test/sub1/subsub1/ me@my-system:~/test/sub1/subsub1
-e
标志接受一个远程shell来执行传输. 在rsync
中,尾随斜杠非常重要,因此请确保您与上面的示例匹配.
The -e
flag accepts a remote shell to use to carry out the transfer. Trailing slashes are very important with rsync
, so make sure yours match the example above.
这篇关于将文件夹scp到远程系统,保持目录布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!