How To Display Just The HTTP Response Code In Command Line Curl

Posted by Artem Russakovskii on June 10th, 2010 in Linux, Tips

Updated: September 8th, 2011

Today, I was looking for a quick way to see HTTP response codes of a bunch of urls. Naturally, I turned to the curl command, which I would usually use like this:

curl -IL "URL"

This command would send a HEAD request (-I), follow through all redirects (-L), and display some useful information in the end. Most of the time it's ideal:

curl -IL "http://www.google.com"
HTTP/1.1 200 OK
Date: Fri, 11 Jun 2010 03:58:55 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Server: gws
X-XSS-Protection: 1; mode=block
Transfer-Encoding: chunked

However, the server I was curling didn't support HEAD requests explicitly. Additionally, I was really only interested in HTTP status codes and not in the rest of the output. This means I would have to change my strategy and issue GET requests, ignoring HTML output completely.

Curl manual to the rescue. A few minutes later, I came up with the following, which served my needs perfectly:

curl -sL -w "%{http_code} %{url_effective}\\n" "URL" -o /dev/null

Here is a sample of what comes out:

curl -sL -w "%{http_code} %{url_effective}\\n" "http://www.amazon.com/Kindle-Wireless-Reading-Display-Generation/dp/B0015T963C?tag=androidpolice-20" -o /dev/null
200 http://www.amazon.com/Kindle-Wireless-Reading-Display-Generation/dp/B0015T963C

Here, -s silences curl's progress output, -L follows all redirects as before, -w prints the report using a custom format, and -o redirects curl's HTML output to /dev/null.

Here are the other special variables available in case you want to customize the output some more:

  • url_effective
  • http_code
  • http_connect
  • time_total
  • time_namelookup
  • time_connect
  • time_pretransfer
  • time_redirect
  • time_starttransfer
  • size_download
  • size_upload
  • size_header
  • size_request
  • speed_download
  • speed_upload
  • content_type
  • num_connects
  • num_redirects
  • ftp_entry_path

Is there a better way to do this with curl? Perhaps, but this way offers the most flexibility, as I am in control of all the formatting.

● ● ●

Artem Russakovskii is a San Francisco programmer, blogger, and future millionaire (that last part is in the works). Follow Artem on Twitter (@ArtemR) or subscribe to the RSS feed.

In the meantime, if you found this article useful, feel free to buy me a cup of coffee below.

  • James Broadhead

    Hey –

    Thanks for this; I needed a minimum-effort way of checking some returned headers, and this really got me up and running in no time at all. Cheers!


  • tyson

    Perfect – exactly what I needed. Thanks for sharing!

  • Alessandro

    Hi man, that worked fine, but i can't get the status code if the request is POST, is that normal?

  • schinta

    Hi, This is a cool thing you've shown. One question though .. can the output of -w argument be redirected into a text file when using this curl command in a batch script ?

    • Abhijeet Kudale

      yes we can do tht u jst have to add > file_path/file_name to it
      eg: curl -sL -w "%{http_code} %{url_effective}\n" "URL" -o /dev/null > /home/Abhijeet/abc.txt

  • David

    Thank you! Just what I needed!

  • Mike

    How I can the last modified time?

  • Charlie


    Exactly what I was looking for

  • Ben Di Maggio

    Thanks Artem — this is a really helpful tool!

  • federico

    just what I was looking for, thanks!

  • andy

    Thanks !
    Exactly what I was looking for

  • Anderson Ferraz

    Amazing, dude! Thank you!

  • Jakub

    thanks for very simple and informative tutorial! helped me a lot!

  • Andreas Taschner

    Thank you _so_ much for sharing this tip !
    Saved me a lot of time.
    Keep them coming :I

  • Adam

    Thanks for the very helpful tutorial! Just wondering if there is a way do determine if 301 redirects are in place properly working without querying one and the same URL multiple times?

  • JB

    Not sure how to suppress the status codes for each response. Here's what I used to manually take them out,

    `curl -is dict://dict.org/d:$1 | awk '{ split("250|221|220|100",a,"|"); for (i=0; i<length(a); i++) { b[a[i]]=1; } if (!($1 in b)) { if ($1=="151") { $1=""; } print; } }'`
    # some dictionary lookup on $1
    # split ignorable status codes into a/b
    # replace good status code (151) with a blank

  • John Tan

    Thanks you've answered my question about how to get curl to just display the HTTP response code.

    • chesteroni

      Only code:
      curl -s -w "%{http_code}\n" "URL" -o /dev/null

  • I'm working with a ton of redirects and just wanted to script up a way to verify them. thanks for this tip! It worked like a charm.

  • JW

    Is there a way to do this with the -K switch to read in a file of URLs?

  • Antoine Wils

    thanks, works perfectly

  • Kevin Jones

    Why cant I get HTTP responses to come back for SSL curls? What am I doing wrong.

    for url in $(cat ev1_https.txt) ; do curl –max-time 30 -sL -w "%{http_code} %{url_effective}\n" "$url" -o /dev/null; done

    • Kevin Jones

      neverming I figured it out I needed to pass -k for IGNORE SSL.

      Thanks anyways!!

  • Pingback: Tales of the Ineffable()

  • Pingback: Fix Curl Error Code 7 Windows XP, Vista, 7, 8 [Solved]()

  • Pingback: Fix Curl Error Code 51 Windows XP, Vista, 7, 8 [Solved]()

  • Pingback: Fix Curl Error Code 58 Windows XP, Vista, 7, 8 [Solved]()

  • Leaprieto

    worked perfect, very useful

  • seb

    Thank you, this helped me today.

  • Good job.