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

pin*_*gul 6 bash shell copy wildcard

是否有可能获得通配符匹配的值?即我想做

scp -r user@host:some/path/with/*/file.txt cp/to/*/file.txt
Run Code Online (Sandbox Code Playgroud)

使用上面的命令,我想要

some/path/with/TEST/file.txt --> cp/to/TEST/file.txt
some/path/with/OTHER/file.txt --> cp/to/OTHER/file.txt
Run Code Online (Sandbox Code Playgroud)

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

在我的服务器上我有结构

server $ pwd
~/sixtrack/simulations
server $ ls
run0001
run0002
run0003
...
Run Code Online (Sandbox Code Playgroud)

run*一个目录,包含文件一样run*/summary.datrun*/tracks.dat等我可以复制一切交给我自己的电脑一样

pingul $ scp -r 'user@host:sixtrack/simulations/*' .
Run Code Online (Sandbox Code Playgroud)

但是,我不想复制每个文件,而只是复制一些。因为所有文件都命名相同,并且仅通过目录分隔,所以我必须尊重结构。因此,我想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' */.
Run Code Online (Sandbox Code Playgroud)

没有完成我想要的。添加-v选项作为建议打印出来

Executing: cp -r -- run0001/. run0100/.
Executing: cp -r -- run0002/. run0100/.
Executing: cp -r -- run0003/. run0100/.
...
Run Code Online (Sandbox Code Playgroud)

我只有一次summary.datrun0100。所有其他目录都是空的(即run0001, run0002, ...)

我想要的可能吗?

Ark*_*zyk 5

这取决于您是想在运行命令* 之前还是运行之后扩展,以及您是否想以交互方式进行扩展。

如果您想在交互式运行命令之前扩展它,您可以使用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.
Run Code Online (Sandbox Code Playgroud)

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

$ pwd
/tmp/expand-glob
$ ls
FILE
$ cp /tmp/expand-*/*
Run Code Online (Sandbox Code Playgroud)

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

$ cp /tmp/expand-glob/FILE
Run Code Online (Sandbox Code Playgroud)

如果您想扩展*到脚本中的测试命令,那么 和 scp都不cp是一个好的选择,因为它们无法在模式下运行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
Run Code Online (Sandbox Code Playgroud)

编辑

这个怎么样:

$ rsync -avn -R --rsync-path="cd sixtrack/simulations && rsync"  user@host:run*/summary.dat .
Run Code Online (Sandbox Code Playgroud)

-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
Run Code Online (Sandbox Code Playgroud)