1

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

Gabe
  • 226
  • 3
  • 13

1 Answers1

2

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
l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • 1
    Perfect. Thank you. just what I needed. Had read printf was preferred for date and time directives over date for bash 4.x. But didn't appreciate there was limitations with stdin. – Gabe Aug 24 '19 at 09:42