简体   繁体   English

将多个目录中的多个文件从服务器复制到本地,文件进入与服务器同名的目录

[英]Copying multiple files in multiple directories from server to local, with files going into directories with the same name as server

Sorry if the title is confusing对不起,如果标题令人困惑

Basically, on my server, I have multiple files for multiple runs of an analysis for multiple traits, eg:基本上,在我的服务器上,我有多个文件用于对多个特征进行多次分析,例如:

/Weight/run1/file1.txt /重量/run1/file1.txt
/Weight/run1/file2.txt /重量/run1/file2.txt
/Weight/run2/file1.txt /重量/run2/file1.txt
/Weight/run2/file2.txt /重量/run2/file2.txt
/LegLength/run1/file1.txt /LegLength/run1/file1.txt
/LegLength/run1/file2.txt /LegLength/run1/file2.txt
/LegLength/run2/file1.txt /LegLength/run2/file1.txt
/LegLength/run2/file2.txt /LegLength/run2/file2.txt

In total I have 11 traits with two runs each, with each run generating 26 files.我总共有 11 个特征,每个特征运行两次,每次运行生成 26 个文件。

I want to copy them across onto my local machine, but keeping the file hierarchy the same.我想将它们复制到我的本地机器上,但保持文件层次结构相同。 I can only find code that copies from multiple directories into one directory, which is not what I want.我只能找到从多个目录复制到一个目录的代码,这不是我想要的。

So far I've managed to hack together the following code from somewhat related stackoverflow questions, though obviously doesn't work and just tries to find ~/Documents/path/to/local/directories on the server.到目前为止,我已经设法从一些相关的 stackoverflow 问题中破解以下代码,但显然不起作用,只是试图在服务器上找到~/Documents/path/to/local/directories I was wondering if anyone had any advice on how to tweak it?我想知道是否有人对如何调整它有任何建议?

`ssh user@server 'for f in /path/to/server/directories/*/*/*txt ; do newpath="${f%/*}"; newpath=${newpath:41}  ; scp user@server:${f} ~/Documents/path/to/local/directories/${newpath};  done'`

I also have multiple other files in each end directory on the server with different extensions, but I am not interested in copying them across.我在服务器上的每个端目录中也有多个其他文件,具有不同的扩展名,但我对复制它们不感兴趣。

(Edit to add: I'm using newpath=${newpath:41} because it removes /path/to/server/directories/ from the variable) (编辑添加:我正在使用newpath=${newpath:41}因为它从变量中删除/path/to/server/directories/

It's a little complex to get it right but a combination of --include and --exclude in the rsync command should be able to do what you want:正确处理它有点复杂,但是rsync命令中的--include--exclude的组合应该能够执行您想要的操作:

rsync -av \
      --include '/Weight/' \
      --include '/Weight/run[12]/' \
      --include '/Weight/run[12]/file[12].txt' \
      --include '/LegLength/' \
      --include '/LegLength/run[12]/' \
      --include '/LegLength/run[12]/file[12].txt' \
      --exclude '/*' \
      --exclude '/*/*' \
      --exclude '/*/*/*' \
      user@server:'/path/to/server/directories/' ./here/

note: in this case, the first / in the includes & excludes means /path/to/server/directories/注意:在这种情况下,包含和排除中的第一个/表示/path/to/server/directories/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM