如何在目标路径中使用带有通配符 (*) 的 scp 进行复制?

How do I copy with scp with a wildcard(*) in the destination path?

是否可以获取通配符匹配的值? IE。我想做

scp -r user@host:some/path/with/*/file.txt cp/to/*/file.txt

通过上面的命令,我想要

some/path/with/TEST/file.txt --> cp/to/TEST/file.txt
some/path/with/OTHER/file.txt --> cp/to/OTHER/file.txt

编辑: 我将添加一些实际设置的更多数据,这可能会使它更清楚。

在我的服务器上我有结构

server $ pwd
~/sixtrack/simulations
server $ ls
run0001
run0002
run0003
...

每个 run* 是一个目录,包含 run*/summary.datrun*/tracks.dat 等文件。我可以将所有内容复制到我自己的计算机上,例如

pingul $ scp -r 'user@host:sixtrack/simulations/*' .

但是,我不想复制 每个 文件,而是只复制一些。因为所有文件的名称都相同并且只是通过目录分隔,所以我必须尊重结构。因此,我希望 host:sixtrack/simulations/run0001/summary.dat 进入我的本地文件夹 /Users/pingul/Workspace/simulations/run0001/summary.dat

运行

pingul $ pwd
/Users/pingul/Workspace/simulations
pingul $ scp -r 'user@host:sixtrack/simulations/*/summary.dat' */.

没有达到我的要求。按照建议添加 -v 选项打印出

Executing: cp -r -- run0001/. run0100/.
Executing: cp -r -- run0002/. run0100/.
Executing: cp -r -- run0003/. run0100/.
...

而我在 run0100 中只得到一个 summary.dat。所有其他目录都是空的(即 run0001run0002、...)

我想要的可能吗?

看你要不要扩* 之前 运行宁一个 命令或 after 运行ning 它以及是否要执行它 交互与否。

如果您想在之前 运行以交互方式扩展它,您可以使用 insert-completions (M-*)glob-expand-word (C-x *) 中描述的 man bash:

   glob-expand-word (C-x *)
          The word before point is treated as a pattern for
          pathname expansion, and the list of matching filenames
          is inserted, replacing the word.  If a numeric argument
          is sup- plied, an asterisk is appended before pathname
          expansion.

   insert-completions (M-*)
          Insert all completions of the text before point that
          would have been generated by possible-completions.

要使用这些功能,请将光标放在 * 之前或之后,然后按 Control-x *Alt-*:

$ pwd
/tmp/expand-glob
$ ls
FILE
$ cp /tmp/expand-*/*

现在将光标放在最后一个 * 之后,不要按 Enter 但是 C-x * 你会得到这个

$ cp /tmp/expand-glob/FILE

如果您想扩展 * 以测试脚本中的命令,那么两者都不是 scpcp 都不是一个好的选择,因为 dry-run 中不能 运行 模式。你应该使用 rsync 这样的东西来显示什么 如果它实际上 运行 是这样的话,它 传输文件:

$ rsync  -vn --relative /tmp/expand-*/* .
/tmp/
/tmp/expand-scp/
/tmp/expand-scp/a
/tmp/expand-scp/b
/tmp/expand-scp/c
/tmp/expand-scp/mmmm32

编辑:

这个怎么样:

$ rsync -avn -R --rsync-path="cd sixtrack/simulations && rsync"  user@host:run*/summary.dat .

-n代表干-运行。使用此选项 rsync 将仅打印如何在当前目录中重新创建远程目录结构。在您的情况下,它将类似于:

receiving incremental file list
drwxr-xr-x          4,096 2016/10/13 12:45:36 run0001
-rw-r--r--              0 2016/10/13 12:23:18 run0001/summary.dat
drwxr-xr-x          4,096 2016/10/13 12:23:18 run00010
-rw-r--r--              0 2016/10/13 12:23:18 run00010/summary.dat
drwxr-xr-x          4,096 2016/10/13 12:23:18 run0002
-rw-r--r--              0 2016/10/13 12:23:18 run0002/summary.dat
drwxr-xr-x          4,096 2016/10/13 12:23:18 run0003
-rw-r--r--              0 2016/10/13 12:23:18 run0003/summary.dat
drwxr-xr-x          4,096 2016/10/13 12:23:18 run0004
-rw-r--r--              0 2016/10/13 12:23:18 run0004/summary.dat
drwxr-xr-x          4,096 2016/10/13 12:23:18 run0005
-rw-r--r--              0 2016/10/13 12:23:18 run0005/summary.dat
drwxr-xr-x          4,096 2016/10/13 12:23:18 run0006
-rw-r--r--              0 2016/10/13 12:23:18 run0006/summary.dat
drwxr-xr-x          4,096 2016/10/13 12:23:18 run0007
-rw-r--r--              0 2016/10/13 12:23:18 run0007/summary.dat
drwxr-xr-x          4,096 2016/10/13 12:23:18 run0008
-rw-r--r--              0 2016/10/13 12:23:18 run0008/summary.dat
drwxr-xr-x          4,096 2016/10/13 12:23:18 run0009
-rw-r--r--              0 2016/10/13 12:23:18 run0009/summary.dat

您要求的是不可能的,因为 cp/scp 命令只接受一个目的地。但是,您可以使用一些非常简单的脚本轻松模拟它:

for d in *
do
    scp -r user@host:some/path/with/"$d"/file.txt cp/to/"$d"/file.txt
done

也可以单行写成

for d in *; do scp -r user@host:some/path/with/"$d"/file.txt cp/to/"$d"/file.txt; done