using curl to access ftp server

October 6th, 2007 mysurface Posted in Advance, curl, Network | Hits: 176989 | 29 Comments »

The title doesn’t sound interesting if you have no idea what is curl. Why we need to use curl to access ftp server, if we can access ftp with tools like ftp in console or gFTP?

Well gFTP is a very handy ftp client with gtk front end, as I use it daily to maintain my files in my web servers. But sometimes we need a command that allows us to put into script, then gFTP is not suitable for that. And default ftp command surprise me that we cannot do things inline. Let say, I wanna download a file from a ftp server by passing the username and password within one line of command, so I can put into my script. I can’t do this with default ftp command!

Curl provides you a way to access ftp server and download, upload files, listing directories and file, and you can write your routine into a script using curl.

Lets look at how we can do it with curl.


The simplest way to access a ftp server with username and password

curl ftp://myftpsite.com --user myname:mypassword 

With the command line above, curl will try to connect to the ftp server and list all the directories and files in the ftp home directory.

To download a file from ftp server

curl ftp://myftpsite.com/mp3/mozart_piano_sonata.zip --user myname:mypassword -o mozart_piano_sonata.zip

To upload a file to ftp server

curl -T koc_dance.mp3 ftp://myftpsite.com/mp3/ --user myname:mypassword

To list files in sub directories.

curl ftp://myftpsite.com/mp3/  --user myname:mypassword

List only directories, silent the curl progress bar, and use grep to filter

curl ftp://myftpsite.com  --user myname:mypassword -s | grep ^d

Remove files from ftp server.
This is a bit tricky, because curl do not support that by default, well anyway, you can make use of -X and pass in the REAL FTP command.
(Check out a list of FTP service Command in rfc 959, under 4.1.3. FTP SERVICE COMMANDS)

curl ftp://myftpsite.com/ -X 'DELE mp3/koc_dance.mp3' --user myname:mypassword

Caution: Make sure you are know what are you deleting! It will not prompts ‘Are you sure?’ confirmation.

Check out curl manual for more,

man curl

or this, ^^ (contains different info)

curl --manual | less 

29 Responses to “using curl to access ftp server”

  1. You might want to look into use a .netrc file to handle your FTP credentials. It makes things much easier when you need to use FTP on the fly or in bash scripts.

    see: http://man.linuxquestions.org/index.php?query=netrc&type=2&section=5
    (the .netrc file must have permissions of 600 to function properly, but it will warn you if you forget)

    You can pass your ftp commands to ftp via a text file if you like as in:

    ftp < textfile

  2. Great! Thanks Techfun

  3. [...] have wrote a post regarding on how to access ftp host using curl. And this time, let us look at how to mount the ftp host to a local directory on top of [...]

  4. expanding on Techfun’s post you can also set macros in the .netrc file. Then it can be executed in a shell script with the command
    echo “$ macroname” | ftp ftplocation
    information on setting up a macro can be found here
    http://www.mavetju.org/unix/netrc.php
    works well for automated offsite backups

  5. I wish to fetch a file from an ftp server via an http proxy, probably squid. Can’t do this with normal ftp, and need to know how to setup the environment so that curl does this. I want to run a script that needs to download some ftp files.

  6. Thanks! This is exactly what I was looking for.
    As relevant in Aug09 as when it was first posted in Oct07.

  7. to do it over proxy

    curl -v -x proxy-server:8080 -U proxyuser:proxypassword –user ftpuser:ftpassword ftp://ftp.server.com

  8. Also, if using a .netrc file, you will need to add the -n option to the curl command (at least in my version).

  9. Certainly invaluable info. Thanks a lot, superb share.

  10. some of the comments above are a bit questionable

  11. Asseguarbub Says:

    Hello. And Bye.

  12. Me new to curl..
    your tutorial has been very veru helpful..

    Thank you.

  13. I used to be suggested this blog through my cousin. I’m not sure whether or not this publish is written by way of him as nobody else realize such special about my trouble. You’re wonderful! Thank you!

  14. This is great article.

  15. Protect your online privacy and browse the web anonymous by changing your IP

  16. typo:
    off course, it should be:
    curl sftp://myftpsite.com/ -Q ‘rm myfile.xyz’ –user myname:mypassword

  17. Thanks, great tutorial. I need this :D

    But, can we delete multiple files on FTP through curl?

  18. Oh my goodness! a tremendous article dude. Thank you However I’m experiencing subject with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting identical rss problem? Anyone who is aware of kindly respond. Thnkx

  19. Download Music Free: http://www.oron.se
    Psychedelic Punk Rap Reggae Rock Rock And Roll

    Soul Soundtrack Techno Thrash Metal Top 40
    Trip-Hop Vocal folk Folk-Rock Folklore Freestyle

  20. http://www.uploaded.lt/ftp.php
    Download Free Music 2013 : Jumpstyle, Pop, Psychedelic, R’n'B, Rock,
    Techno, Trance, Others, Alternative, Ambient, Avantgarde, Bass

  21. Download Music: http://www.oron.at
    Tech House-Electro
    Techhouse Techno Techno-Industrial
    Techno-Trance
    Terror Thrash Metal
    Top 40 Trailer Trance
    Trap Trash Metal Tribal
    Trip-Hop Uncategorized
    Vocal

  22. What a beautiful set of examples for the predicament I was in, which was implementing Secure FTP from Linux using Perl. My old Perl Secure FTP module would not work in my app, and I had to resort to using system commands from Perl. By adding the –ftp-ssl -v -k switches, it did the job. Your -X DELE example save me trouble too, because I was trying to use -Q DELE and that did not fly.

    Thank you so much.

  23. hi,

    Any ideas on how to validate the response code from curl for the ftp process?

    I’ve written a script to ftp files into a remote server. but the script has more content than uploading files. I want to make sure the rest of the script runs only if curl returns 9 response from the ftp.

    Thanks in advance

  24. wonderful submit, very informative. I’m wondering
    why the other specialists of this sector don’t notice this.
    You must proceed your writing. I’m confident, you’ve a great readers’ base already!

  25. Car Insurance Discount Car insurance is one part of our family funds that we
    will at all times need to cope with and so it behooves
    us to get higher knowledgeable. Absence of car insurance eliminates any likelihood of help from
    third get together insurance company in the settlement of claim in your aspect even when there is
    no such thing as a fault of yours.

    my weblog car insurance for young drivers – Fidelia,

  26. Oh my goodness! Amazing article dude! Many thanks, However I am going through problems with your
    RSS. I don’t know the reason why I am unable to join it.

    Is there anybody getting identical RSS issues?
    Anyone who knows the solution will you kindly respond? Thanks!!

  27. It should come as no surprise, then, that Pinterest is
    fast becoming a top social networking tool. I recommend using a simple font that is easy to
    read in a size ranging from 32 to 40. See the action on the left hand column of the home
    page.

Leave a Reply