DEV Community

loading...

cURL for FTP

Naveen
Software Engineer, Learner by experiments, Finds a kick out of solving problems, Blogger, Chess player, CallOfDuty Player, Loves biking
・2 min read

cUrl for FTP

cUrl is a terminal based client for data exchange over URL syntax.

I knew this definition about curl and I was usually using curl against http endpoints with a several combination of options. There was a time at work where I had to troubleshoot an issue in production and logs were shared over an ftp server path. Little did I know that this little tool would just work with ftp endpoints as well. So, here are some of the combinations I used curl to work on ftp endpoints. These are just starting points to give quick start and there are many more scenarios as well.

Directory listing

Straight forward. Just remember that the directory to be listed would be the home directory of the authenticated user. Output would be list of all files & directories in the home directory of the authenticated user. While trying out listing, try using --list-only to only return the name of remote objects.

curl "ftp://<ftp-host>:21" --user <ftp-user-id>:<ftp-user-password>
Enter fullscreen mode Exit fullscreen mode

Upload a local file to remote

Option --upload-file or -T, in short, is used to upload files.

curl --upload-file ./<local-filename> "ftp://<ftp-host>:21/<remote-filename>" --user <ftp-user-id>:<ftp-user-password>
Enter fullscreen mode Exit fullscreen mode

Download a remote file to local

Option --output or -o (lowercase) is used to collect the content from the remote stream.

curl "ftp://<ftp-host>:21/<remote-name>" -o ./<local-filename>  --user <ftp-user-id>:<ftp-user-password>
Enter fullscreen mode Exit fullscreen mode

Additional point to remember is to consider -O (uppercase) when the file has to be downloaded with the same name as in remote.

Deleting a remote file

Recollect that use of -X option used to set the method name like GET, PUT, POST, DELETE against http endpoints. It is because http server had commands named like DELETE. In similar lines, FTP server has a command DELE <file-to-delete> to purge the given file.

curl "ftp://<ftp-server>:21" -X "DELE remote-file"  --user <ftp-user-id>:<ftp-user-password> -v
Enter fullscreen mode Exit fullscreen mode

Suggestions

  • Use the option -v for debugging. There is a lot to understand out of the request and response once this debugging switch is enabled.
  • FTP server I used was not over ssl. In case, the remote FTP server is over ssl, use the switch --ftp-ssl.

That's it !

But yes, there are a lot more scenarios like handling directory recursion, purging a directory, handling binary content. But then the idea is that curl works well on ftp protocols. That makes it easy to script. Then there is no need of a dedicated application for working on FTP requests/responses.

Discussion (0)