A guide on how to use FTP with curl

curl is the goto tool for anything HTTP related. A lesser known feature about curl is how you can use it for your FTP and FTPS tasks and that’s what we will be covering in this guide, some example of common curl command you can use to:

  1. upload files on your FTP server
  2. download file stored on an FTP server
  3. authenticate to your FTP server
  4. list files on your FTP server
  5. delete files on your FTP server

List files on an FTP server

~/$ curl ftp://ftp.slackware.com
lrwxrwxrwx 1 0 0 17 Jun 15 2015 debian -> pub/debian/debian
lrwxrwxrwx 1 0 0 18 Jun 15 2015 debian-cd -> pub/debian-cdimage
lrwxrwxrwx 1 0 0 18 Jun 15 2015 debian-cdimage -> pub/debian-cdimage
drwxr-xr-x 2 0 0 56 May 12 2016 lost+found
drwxr-xr-x 4 0 0 4096 Jun 30 16:58 pub
-rw-r--r-- 1 0 0 754 May 13 2015 welcome.msg
~/$ curl ftp://ftp.slackware.com/debian-cd/
drwxr-sr-x 9 3064 1199 4096 Sep 27 00:23 10.6.0
drwxr-xr-x 5 3064 1199 58 Sep 27 00:23 10.6.0-live
lrwxrwxrwx 1 3064 1199 6 Sep 27 00:29 current -> 10.6.0
lrwxrwxrwx 1 3064 1199 12 Sep 27 00:29 current-live -> 10.6.0-live/
-rw-r--r-- 1 3064 1199 15496 Sep 29 04:12 ls-lR.gz
drwxr-xr-x 4 3064 1199 42 May 23 2005 project

Downloading files on an FTP server

~/$ curl ftp://ftp.slackware.com/welcome.msg
---------------------------------------------------------------------------
R S Y N C . O S U O S L . O R G
Oregon State University
Open Source Lab

Unauthorized use is prohibited - violators will be prosecuted
---------------------------------------------------------------------------

For more information about the OSL visit:
http://osuosl.org/services/hosting

This host is the home to the primary archives of several
projects. We would prefer that only primary/secondary
mirrors use this service. Thanks!

---------------------------------------------------------------------------
~/$ curl -O "ftp://ftp.slackware.com/{welcome.msg,debian-cd/ls-lR.gz}"
[1/2]: ftp://ftp.slackware.com/welcome.msg --> welcome.msg
--_curl_--ftp://ftp.slackware.com/welcome.msg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 754 100 754 0 0 347 0 0:00:02 0:00:02 --:--:-- 347

[2/2]: ftp://ftp.slackware.com/debian-cd/ls-lR.gz --> ls-lR.gz
--_curl_--ftp://ftp.slackware.com/debian-cd/ls-lR.gz
100 15496 100 15496 0 0 12201 0 0:00:01 0:00:01 --:--:-- 28537
~/$ ls # verify the files have been downloaded
ls-lR.gz welcome.msg

Connect to a FTP server with your credentials

~/$ curl ftp://username:password@ftp.slackware.com/
curl: (67) Access denied: 530
~/$ curl -u "username:password" ftp://ftp.slackware.com/
curl: (67) Access denied: 530
~/$ curl -u "anonymous:password" ftp://ftp.slackware.com/
lrwxrwxrwx 1 0 0 17 Jun 15 2015 debian -> pub/debian/debian
lrwxrwxrwx 1 0 0 18 Jun 15 2015 debian-cd -> pub/debian-cdimage
lrwxrwxrwx 1 0 0 18 Jun 15 2015 debian-cdimage -> pub/debian-cdimage
drwxr-xr-x 2 0 0 56 May 12 2016 lost+found
drwxr-xr-x 4 0 0 4096 Jun 30 16:58 pub
-rw-r--r-- 1 0 0 754 May 13 2015 welcome.msg

Uploading some files to an FTP server

~/$ curl -T welcome.msg -u "username:password" ftp://ftp.example.com/
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 754 0 0 100 754 0 194 0:00:03 0:00:03 --:--:-- 194

Delete files on an FTP server

~/$ curl -u "username:password" --quote "DELE welcome.msg" ftp://ftpperso.free.fr