2

curl is able to download a URL to specific file with the -o/--output switch. AFAIk this will save this file as it goes, and if something happens to the download, then the file will only be half downloaded.

I can work around this, by creating a temp file, and moving that in place:

TEMP=$(mktemp -p /data/path/to/results/ tmp.results.zip.XXXXXX)
curl -o ${TEMP} https://example.com/files/results.zip
mv ${TEMP} /data/path/to/results/results.zip

Can I do this all in one command just with curl? Does curl have any option for “Save to temp file & only move over if succeeded?”

Update: I often use set -o errexit, so if the curl command fails, the script will fail. mv run iff curl didn't fail.

5
  • 1
    You could just replace "mv ${TEMP} /data/path/to/results/results.zip" with if [ $? -eq 0 ] then mv ${TEMP} /data/path/to/results/results.zip fi
    – summertime
    Jun 19, 2020 at 8:58
  • 1
    @summertime good catch! 🙂 I've updated the question to explain that I use errexit to prevent that Jun 19, 2020 at 9:24
  • Thanks Rory! Would this work: replace "mv ${TEMP} /data/path/to/results/results.zip" with: if [ -e $TEMP ] then mv ${TEMP} /data/path/to/results/results.zip fi
    – summertime
    Jun 19, 2020 at 9:40
  • 1
    @summertime the file will exist even if the download fails. I don’t think there’s anything better than the errexit approach. Jun 19, 2020 at 10:12
  • @Stephen Kitt, ok good to know.
    – summertime
    Jun 19, 2020 at 10:22

1 Answer 1

3

As far as I know, curl doesn’t have this built-in; your approach is as good as it gets (with errexit). You can make it explicit, regardless of the settings, with

curl -o ${TEMP} https://example.com/files/results.zip &&
mv ${TEMP} /data/path/to/results/results.zip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.