Joho the Blog » Beginner-to-Beginner: FTP via curl

Beginner-to-Beginner: FTP via curl

Cyberduck works well for most of my FTP’ing needs. But sometimes I want to be able to automate an upload or a download without having to go through a graphical user interface. The Mac comes with a handy tool called curl. Unfortunately, I’ve had trouble finding instructions comprehensible to one such as I. (Here’s man page for curl. Of course, you can also see the man page by typing man curl into a terminal window. As usual, the man page is efficient to the point of being terrifying.) So, here’s a beginner-to-beginner guide. As always, use this carefully because I don’t know what I’m talking about.

To begin, open up a terminal window. Type curl -V into it and hit return just to make sure that curl is there. If it is, it should tell you what version of curl you’re running.

Excellent!


If you want to upload a file called test.txt to the mydir directory on your myserver server, and if your username is uname and your password is pwd, then you’d type the following into your terminal:

curl -v -T test.txt ftp://uname:pwd@myserver/mydir/

The -v turns on verbose mode so you can see all the errors I’ve made in these instructions. Once it works, you can replace the -v with -s to silence the messages.

If you want to download a file, use -O instead of -T.

If you want to do this by double clicking on a file in the Finder, you have to go through a few more steps.

First, type into a text editor the same line that you were typing into the terminal, except make sure that you give the full pathname of the file you want to upload. Save the file with .command as the extension (e.g., autoftp.command). In the terminal go the directory where you’ve saved the file and type chmod a+x autoftp.command. This makes the file executable (runnable). Now double clicking it in the Finder should run it.

There’s lots more you can do with curl. If you figure out how to do it let me know. And, of course, I’ll happily correct this post with the corrections you point out in the comments… [Tags: ]

9 Responses to “Beginner-to-Beginner: FTP via curl”

  1. I use curl extensively, for instance to automate gathering of networked device configurations through their web interface.
    By using its cookie-saving features, you can use curl to automatically navigate into web apps that require login.

    If you use the option “-c cookies.txt”, a file containing the cookies used in the transaction will be saved as cookies.txt.

    If you use “-b cookies.txt” in subsequent calls, the cookies will continue to be be read and written during those transactions.

    That is to say, if you were to want to press the reboot button on a home router from a script, it might look something like this:

    #!/bin/sh
    # script to reboot fictitious router.

    # submit login form
    curl \
    -sc cookies.txt \
    http://192.168.1.1/Login \
    -d “username=admin” \
    -d “password=secret” \
    -d “submit=Login”

    # press reboot button on admin page
    # this would fail if we didn’t have the cookie
    # we saved from the login
    curl \
    -sb cookies.txt \
    http://192.168.1.1/Admin \
    -d submit=Reboot

  2. When checking to see if curl is installed and in your path, you may want to recommend the command
    curl -V

    With the capital V option, curl will display it’s version information instead of the “error” it displays when run with no options.

    Also, you explain that an executable file is runnable, but you don’t explain how to open the terminal (you’ll find it in Applications->Utilities) or how to change directories (cd /your/directory/path/). Just thought that might be valuable to some people who want to try this.

    Finally, as a little background, “cURL is a command line tool for transferring files with URL syntax, supporting FTP, … HTTP, HTTPS, … [and] SFTP…” (Wikipedia article on cURL)

    As Brent explains above, the curl application can be used to simulate a series of actions, such as rebooting a home wireless router that uses a web interface to control it.

    You could also use the curl application to automate other types of work. For instance, let’s say you wanted to keep track of which candidate had the most Google results for their name. You might create a script that uses curl to submit the candidates names to Google one by one, and then saves the results page to your hard drive. You could automate the running of that script using an iCal alarm, or a cron job for more advanced users. After some time goes by, you could then tabulate the results. Because you’re automating your data collection, why stop with just Google? You could also collect the results from other search engines too!

    The world of automation is a fantastic place, and OS X comes with a variety of tools right out of the box that let you be very effective. Thanks for this post David. You might also want to check out some basic Apple Scripts, or even the Automator tool (Applications->Automator).

  3. Jason, I changed the line about how to check if you have curl to reflect your comment. Thanks!

  4. everyone talks about uploading a file can someone give the commands for downloading a file using curl from an ftp server.

  5. hi all,
    I am using curl for web-server application and I am at client side. I
    want to delete file and upload new file using curl command. I tried
    uploading file like this

    curl -T hi.c http://username:password@192.163.1.90/

    and i got it.

    But how to do delete a file using curl command ? please anyone knows
    about
    this ? give me solution

    I want it for http server.
    please help me out.

  6. hi jaya
    you can send some command with curl to the remote ftp
    for delete test with this
    curl -Q “dele ” filetodeletonthe remote

  7. […] How did you install wput? From the error message you got, it doesn't look like it's simply missing a library. It looks more like it's for a different platform or something. The error message "-ash: file: not found" means that the computer does not have the file command installed. I would first try to check if there are some options for ftp installed on the computer. Have you tried the command "ftp"? If it's not installed, maybe it has "curl"? "wput" is not very common because commands like curl do the same and more. If you have curl, you can read this guide on how to use it for ftp uploads. http://www.hyperorg.com/blogger/2008…-ftp-via-curl/ […]

  8. Thanks a lot

  9. Why users still use to read news papers
    when in this technological globe the whole thing is presented on web?

Leave a Reply


Web Joho only

Comments (RSS).  RSS icon