bash 如何使用 scp -r 获取下载的文件列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13742296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get files list downloaded with scp -r
提问by hsz
Is it possible to get files list that were downloaded using scp -r?
是否可以获取使用下载的文件列表scp -r?
Example:
例子:
$ scp -r $USERNAME@HOSTNAME:~/backups/ .
3.tar 100% 5 0.0KB/s 00:00
2.tar 100% 5 0.0KB/s 00:00
1.tar 100% 4 0.0KB/s 00:00
Expected result:
预期结果:
3.tar
2.tar
1.tar
采纳答案by Lee Netherton
The output that scpgenerates does not seem to come out on any of the standard streams (stdout or stderr), so capturing it directly may be difficult. One way you could do this would be to make scpoutput verbose information (by using the -vswitch) and then capture and process this information. The verbose information is output on stderr, so you will need to capture it using the 2>redirection operator.
scp生成的输出似乎没有出现在任何标准流(stdout 或 stderr)上,因此直接捕获它可能很困难。您可以执行此操作的一种方法是scp输出详细信息(通过使用-v开关),然后捕获并处理此信息。详细信息在 stderr 上输出,因此您需要使用2>重定向运算符来捕获它。
For example, to capture the verbose output do:
例如,要捕获详细输出,请执行以下操作:
scp -rv $USERNAME@HOSTNAME:~/backups/ . 2> scp.output
Then you will be able to filter this output with something like this:
然后,您将能够使用以下内容过滤此输出:
awk '/Sending file/ {print $NF}' scp.output
The awkcommand simply prints the last word on the relevant line. If you have spaces in your filenames then you may need to come up with a more robust filter.
该awk命令只是在相关行上打印最后一个单词。如果您的文件名中有空格,那么您可能需要想出一个更强大的过滤器。
回答by jfg956
I realise that you asked the question about scp, but I will give you an alternative solution to the problem Copy files recursively from a server using ssh, and getting the file names that are copied.
我知道你问了关于 的问题scp,但我会给你一个替代解决方案来解决这个问题:使用 ssh 从服务器递归复制文件,并获取复制的文件名。
The scpsolution has at least one problem: if you copy lots of files, it takes a while as each file generates a transaction. Instead of scp, I use sshand tar:
该scp解决方案至少有一个问题:如果您复制大量文件,则每个文件都会生成一个事务需要一段时间。而不是scp,我使用ssh和tar:
ssh $USERNAME@HOSTNAME:~/backups/ "cd ~/backups/ && tar -cf - ." | tar -xf -
With that, adding a teeand a tar -tgives you what you need:
有了这个,添加 atee和 atar -t给你你需要的:
ssh $USERNAME@HOSTNAME:~/backups/ "cd ~/backups/ && tar -cf - ." | tee >(tar -xf -) | tar -tf - > file_list
Note that it might not work in all shell (bash is ok) as the >(...)construct (process substitution) is not a general option. If you do not have it in your shell you could use a fifo (basically what the process substitution allows but shorter):
请注意,它可能不适用于所有 shell(bash 可以),因为>(...)构造(进程替换)不是通用选项。如果您的 shell 中没有它,您可以使用 fifo(基本上是进程替换允许的但更短的):
mkfifo tmp4tar
(tar -xf tmp4tar ; rm tmp4tar;) &
ssh $USERNAME@HOSTNAME:~/backups/ "cd ~/backups/ && tar -cf - ." | tee -a tmp4tar | tar -tf - > file_list
回答by user1097790
scp -v -r yourdir orczhou@targethost:/home/orczhou/ \
2> >(awk '{if(##代码## ~ "Sending file modes:")print }')
with -v "Sending file modes: C0644 7864 a.sql" should be ouput to stderr
使用 -v "发送文件模式:C0644 7864 a.sql" 应该输出到 stderr
use 'awk' to pick out the file list
使用 'awk' 选择文件列表

