Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .