Download multiple Curl pages at the same address without overwriting

Asked

Viewed 138 times

0

I need to download this page several times, it returns a different result each time it is accessed:

i="0"

while [ $i -lt 10 ]; do
    curl -O http://httpbin.org/bytes/128
    i=$[$i+1]
done

But each time the Curl command is executed, the previous file is overwritten, since the name is equal.

How do I not overwrite? The names could be sequential ex. "128 (1)", "128 (2)", ..., "128 (n)".

2 answers

1

Good evening, well Curl can be used with -o to set a file to output, as explained in stack overflow international: https://stackoverflow.com/questions/13735051/capture-curl-output-to-a-file

Curl -K myconfig.txt -o output.txt

Writes the first output Received in the file you specify (overwrites if an old one exists).

Curl -K myconfig.txt >> output.txt

Appends all output you receive to the specified file.

So you can use the declared variable to name the files.

1


Use the option -o (lower case) to specify where to write the response and use your counter as the file name:

i=0; 
while [ $i -lt 10 ]; do 
    curl http://httpbin.org/bytes/128 -o 128_$i
    i=$[$i+1]
done

Or more clearly using for:

for i in {1..10}; do 
    curl http://httpbin.org/bytes/128 -o 128_$i
done

Or more often using seq:

seq -f 'curl http://httpbin.org/bytes/128 -o 128_%g' 9 | bash

Browser other questions tagged

You are not signed in. Login or sign up in order to post.