nulltrap.com

2007/09/19

Programmatic Downloading Files with Curl

Filed under: HOWTO, Utility — Dan @ 10:07

I am asked so many times how to programmatically (aka systematically have a computer sort the details out) download something it amazes me. So I posted my answer here. 

Curl is a nice command line interface program for retrieving files off of the internet.

I primarily use it to download files automagically. Here is a simple example:

curl -o postgresql-8.2.4.tar.gz http://ftp8.us.postgresql.org/postgresql/source/v8.2.4/postgresql-8.2.4.tar.gz


 
This downloads the file archive from the http server, and places it in the current working directory.

Curl has downloads in Windows, Linux, and OS X.

In its default state, it will simply dump the raw HTML code to the standard out (usually the screen). Did I mention how handy this is for scraper and scrubber programs? Another wonderful use of it is to check to see if a web server is alive by referencing the HTTP codes. EG, if a web page returns a 404, then you know the server / service is down!

Cool. So now using one small tool, we made something which we can see if a webserver is alive or not, but also can download things. If the script discovers that the webserver is down, you can have the script do something (email you, log it to a file, send you an sms text message over email, send it to twitter whatever..) 

Good tool in the toolbox!

WP All rights reserved, Copyright 2007-2009 nulltrap.com Page rendered in 0.582 seconds