skip to Main Content

I am working on a bash script and curl to download file from a server. I am trying to add date in format $(date +"%y-%m-%d") to the file that will be downloaded. Below is my curl call

curl --fail -S -O -C - --location "${HEADERS[@]}" -X GET "$url"

With above call the file is downloading fine (example filename: test.txt). Now I want to add date to the file. I think if we use -o option then we can add it like curl -o test-$(date +"%y-%m-%d").txt but in my case I want to store the filename as it is from remote file name. Is it possible to do the same with -O option?
Also if we add the date to remote file name will it affect the -C - option? Can we still have the resuming interrupted downloads feature?

Expected output:
Filename after downloading: test-YYYY-MM-DD.txt

P.S: Please let em know if any info is missing. I am working on Ubuntu and the script will be part of BusyBox system. Also, sorry if the question is more of a discussion.

2

Answers


  1. What I would do:

    file=subscriptionList.opml 
    curl -o "${file%.*}_$(date +%Y-%m-%d).${file#*.}" -s "$url" -C -
    ls
    

    subscriptionList_2023-05-11.opml 
    

    Login or Signup to reply.
  2. You could use:

    $ url="https://example.com/media/document.txt"
    $ curl -o "$(basename ${url%.*}-$(date +"%Y-%m-%d").${url##*.})" -s "$url" -C -
    

    Whether or not the transfer can be resumed depends on if the server supports byte ranges (you can check by omitting -s).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search