Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get files list downloaded with scp -r

Tags:

bash

scp

Is it possible to get files list that were downloaded using 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
like image 827
hsz Avatar asked Oct 20 '25 19:10

hsz


2 Answers

scp -v -r yourdir orczhou@targethost:/home/orczhou/ \
2> >(awk '{if($0 ~ "Sending file modes:")print $6}')

with -v "Sending file modes: C0644 7864 a.sql" should be ouput to stderr

use 'awk' to pick out the file list

like image 59
user1097790 Avatar answered Oct 22 '25 10:10

user1097790


The output that scp generates 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 scp output verbose information (by using the -v switch) 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.

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 awk command 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.

like image 44
Lee Netherton Avatar answered Oct 22 '25 09:10

Lee Netherton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!