EmbLogic's Blog

using curl to access ftp server

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>