Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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