Datasheet
Book VIII
Chapter 1
. . . And UNIX Lurks
Beneath
693
UNIX Programs That Come in Handy
competent at all the standard network protocols. To see it in action, pass a
web address (or URL, to The Enlightened) to the curl command:
curl http://www.mlcbooks.com
The result is that you see the HyperText Markup Language (HTML) page
that’s located at www.mlcbooks.com. Because this isn’t particularly useful
for most people (it’s not very easy to read), you need to add the letter o as a
flag. This specifies where you would like to save this file upon download. To
save the HTML page to your Home directory, add the -o flag and a path to
the destination file.
Don’t forget to precede all flags with a hyphen. For this example, it would
be -o.
curl -o ~/mlcbooks.html http://www.mlcbooks.com
If you now perform an ls command, you see that curl has, in fact, down-
loaded the HTML found at www.mlcbooks.com and saved it to a file named
mlcbooks.html in your Home directory.
The beauty of curl is that it does much more than just retrieve web pages:
It’s equally comfortable with FTP transfers. FTP is used to download (or
receive) files from a server as well as upload (or send) them. Like the previ-
ous HyperText Transfer Protocol (HTTP) examples, you only have to pro-
vide an FTP address in Uniform Resource Locator (URL) format, and curl
will take care of the rest. Of course, most people want to save any files that
they download via FTP — not view them in the Terminal as I did the HTML
file. Therefore, as in the previous example, you should add the -o flag and
a path to the destination of your download. This time, I download a README
file about curl directly from the makers of curl.
curl -o ~/Desktop/README.curl ftp://ftp.sunet.se/pub/www/utilities/curl/README.
curl
If you’re familiar with FTP, you might be wondering whether curl can
upload, too. Yes, indeed! Instead of using the -o flag, you need to use two
flags: -T and -u. The -T flag denotes which file you want to upload. The -u
flag denotes the username and password. Then, specify the FTP destination
address of where you want to upload it. Because this example deals with an
upload, the remainder of this example is for an imaginary FTP server. In real
life, you’d use the appropriate FTP address, username, and password for an
FTP server where you are allowed to upload.
curl -T /Desktop/README.curl -u username:passwd ftp://ftp.yoursitehere.com
/myfiles/README.curl
This example uploads the README.curl file from the Desktop folder that I
downloaded earlier to an imaginary FTP server.
55_9781118022061-bk08ch01.indd 69355_9781118022061-bk08ch01.indd 693 8/10/11 9:39 AM8/10/11 9:39 AM