简体   繁体   中英

How to print ftp file information using curl and printf

I am trying to get file information for batch downloading and having difficulty with pipes to printf.

Test case

$ curl -sI ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp
Last-Modified: Wed, 12 Aug 2015 15:47:26 GMT
Content-Length: 2037
Accept-ranges: bytes

A simple grep to cut that is working for file size

$ curl -sI ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp | grep Content-Length | cut -d ' ' -f 2
2037

Grep to cut is working for file date and time

$ curl -sI ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp | grep Last-Modified | cut -d ' ' -f 3,4,5,6
12 Aug 2015 15:47:26

However, extending grep to cut to printf is not working for the server file date and time, printf is return the local system date and time?

$ curl -sI ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp | grep Last-Modified | cut -d ' ' -f 3,4,5,6 | printf '%(%Y-%m-%d %H:%M:%S)T '

Local date and time, not remote date and time?

2019-08-24 20:32:53

The file size and date will be used in scripts for batch downloads and custom logging. Something along these lines...

infilesize=$(curl -sI $inpath/$infile | grep Content-Length | cut -d ' ' -f 2)
infiledate=$(curl -sI $inpath/$infile | grep Last-Modified | cut -d ' ' -f 3,4,5,6 | printf '%(%Y-%m-%d %H:%M:%S)T ' )
printf $infilesize 2>&1 | tee -a $logpath/$logfile
printf $infiledate 2>&1 | tee -a $logpath/$logfile

My first hurdle is fix the pipe syntax for printf?

Open to other pipe approaches, sed and awk.

Relatively inexperienced with bash, so appreciate verbose code suggestions and or elegant code and explanations, wanting to learn good techniques.

Thanks in advance.

Update

The date subshell approach is working for remote server file date and time.

However, I am now having problems with printf to output multiple variables on a single line. I have had a look around but having difficult understanding the arguments handling for printf and receiving unexpected results.

printf $infilename $infilesize $infiledate 2>&1 | tee -a $outpath/$logfile.txt

This has been asked as a separate question How can I print multiple variables using printf

You don't even need printf , just use a subshell from date to display the cut date information:

curl -sI ftp://ftp.ncbi.nlm.nih.gov/pub/README.ftp | grep Last-Modified | \
date -d "$(cut -d ' ' -f 3,4,5,6)" "+%Y-%m-%d %H:%M:%ST"

Result:

2015-08-12 15:47:26T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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