bash: get http response codes for a list of URLs

I had a file with a list of URLs, and I wanted to grab the HTTP response codes for each of them. I’m sure this quick bash script isn’t the best way to do it, but it works, and I’ll probably want to do this again someday, so here it is!


while read line    
    echo $(curl --write-out %{http_code} --silent --output /dev/null $line)
done <$1 

  • Christian G. Warden

    I used something similar to test whether redirects were set up properly for a bunch of URLs.
    curl’s –location and –write-out %{url_effective} were helpful:

  • Andrew Gilmartin

    Would adding –head, which would, under normal circumstances, make the request faster, also give the same results you needed?

    • Hilary Mason

      Yes, nice catch! Thanks!

  • brocknoland

    Nice!! It solves the problem at hand, which is the important item.

    I typically use xargs when I want to do something on a list. It’s kind of like the functional programming map function.

    xargs -I {} curl -I –write-out %{http_code} –silent “{}” –output /dev/null < $1

  • Vivek Krishna

    If you want to run it in parallel, 
         cat urls.txt | xargs -n 1 -P NoOfThreads -I {} curl –write-out %{http_code} –silent –output /dev/null {}

    • Ken Egozi

      Liked, even though it would consume a thread per parallelism level, which is wasteful as most of what the thread does is wait for network IO.

      with large inputs it would make sense to use a scripting language that allow for easy async IO in non-ugly syntax. Too late to come up with one otoh :)

      • teepark

        actually it’ll run “NoOfThreads” *processes*, not threads.

        but yeah, if you really need serious parallelism then you can probably be bothered to write more than one line.

      • Jason A

        *cough* nodejs *cough*

        • Hilary Mason

          Jason, can you elaborate on that? What’s the advantage of doing this with node.js?

  • E A

    Thanks for this. I’ve been looking for how to do this for a long time.

    Some comments:

    1) #!/usr/bin/env bash is more portable than “#!/bin/bash”

    2) using $() is slower than just using && echo

    3) This isn’t bash specific – it will work on any shell

    4) Using –head would be useful, but may cause different results to GET results.

    • Guest

      or even using #!/bin/sh

  • Giti Shorish

    This is cool!

  • Mcgowan

    anything that “< $1" could just as well be preceded with "cat ${*:–}", the canonical idiom for "all the named files OR the standard input".  by brian kernighan and rob pike's 1984 "the unix programming environment"

  • Mandragola

    4 lines .. poetry ;)