Only output curl result dependent on mime-type

0

Is it possible to make curl only save the output of curl -o if the file is of a certain mime-type?

For example, I might use the following command to download a bunch of images off a server...

curl -o file_#1.jpg http://wwww.mysite.com/images.php?id=[1-50]

On that site, it may return text/html if there isn't an image with that ID. Is there any way to filter the curl results so it only saves files of type image/jpeg?

Alex Coplan

Posted 2011-11-19T12:11:34.727

Reputation: 740

Answers

0

Here is one way to do it in bash:

declare -i i=1
while (( $i <= 50 ))
do
    filename="file_${i}.jpg"
    curl -o ${filename} http://wwww.mysite.com/images.php?id=${i}
    if ! file ${filename} | grep -qi image
    then
      rm -f ${filename}
    fi
    i=$(($i + 1))
done

This loops through all 50 potential images, downloads each, checks if it really is an image and deletes the downloaded file it if isn't.

If your platform has the seq command, you can simplify the loop this way:

for i in $(seq 50)
do
    filename="file_${i}.jpg"
    curl -o ${filename} http://wwww.mysite.com/images.php?id=${i}
    if ! file ${filename} | grep -qi image
    then
      rm -f ${filename}
    fi
done

In newer bash versions, you can also use the {n..m} construct:

for i in {1..50}
do
    ...
done

Adam Zalcman

Posted 2011-11-19T12:11:34.727

Reputation: 208