scp 保留文件属组_将文件夹scp到远程系统,保持目录布局

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

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.

I have tried using scp as follows:

scp -r ./test/sub1/subsub1 me@my-system:~/test/sub1/subsub1

The result: scp: /test/sub1/subsub1: No such file or directory

I also tried:

scp -r ./test/sub1/subsub1 me@my-system:~/test

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'

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值