How do I use a hexadecimal range when downloading multiple files in Curl?

1

I know that I can download a sequence of files using decimal numbers in curl:

curl site.com/file[000-100].jpg -o "file#.jpg"

But I need to download a hexadecimal series of files named file0x000 to file0x254. Can I specify this in one command line? Or can someone help me with a bash script?

Alan in Oakland

Posted 2019-03-04T23:33:15.027

Reputation: 11

2Did you mean file0x000 to file0x0FF? file0x000 to file0x254 doesn't look hexadecimal... – xenoid – 2019-03-05T01:22:20.280

Answers

0

Just put the curl in an for iterator loop like this; save this file as some name—like test.sh and make sure that is has executable permissions—then run that file as ./test.sh:

#!/bin/bash

for i in {0..254}; do
  filename=$(printf file0x%03d.jpg $i);
  echo curl site.com/${filename} -o "${filename}";
done

The above works for me in macOS Mojave (10.14.3) as well as RedHat 7 and Ubuntu 16.04. But the following is slightly simpler, but will only work correctly on RedHat and Ubuntu; on macOS there are no padded numbers:

#!/bin/bash

for i in {000..254}; do
  curl site.com/file0x$i.jpg -o file0x$i.jpg;
done

JakeGould

Posted 2019-03-04T23:33:15.027

Reputation: 38 217

@AlaninOakland You’re welcome! If this answer has helped you, please be sure to upvote it. And if it is the answer that has indeed answered your question, please be sure to check it off as such. – JakeGould – 2019-03-05T00:11:06.443

I think you mean file0x%03x.jpg and not file0x%03d.jpg. Otherwise the printf isn't necessary since you can generate the left-padded numbers with brace expansion using {000..254}. – xenoid – 2019-03-05T01:20:07.273

I believe the original poster made a mistake when asking about hexadecimal and truly means file0x000 to file0x254; so d is appropriate. Regarding using brace expansion using {000..254} how exactly would that work in a per number Curl call like this? – JakeGould – 2019-03-05T01:39:02.417

1@JakeGould for i in {000..254} ; do curl site.com/file0x$i.jpg -o file0x$i.jpg ; done – xenoid – 2019-03-05T07:16:24.437

@xenoid That does not result in padded output for me in macOS. Does work on RedHat. Stand my my method that adds little to negligible overhead and works cross platform. – JakeGould – 2019-03-05T15:05:58.880