cURL transfers files over FTP or SFTP. While FTP is unencrypted and suitable for non-sensitive data, SFTP uses SSH for secure encryption, protecting credentials and content.

Specify the protocol in the URL and provide credentials to upload, download, or list files. Choosing FTP or SFTP accommodates various environments, balancing simplicity and security.

SFTP ensures encryption, suitable for sensitive data, while FTP might suffice in controlled networks. Switching protocols involves adjusting the URL and authentication options.

Steps to use cURL with FTP and SFTP:

  1. Upload a file to FTP.
    $ curl --upload-file example.txt "ftp://ftp.example.com/public_html/" --user username:password

    --upload-file and --user authenticate and transfer files.

  2. Download a file from FTP.
    $ curl --remote-name "ftp://ftp.example.com/public_html/example.txt" --user username:password

    --remote-name saves the file locally with the same name.

  3. List files on FTP.
    $ curl "ftp://ftp.example.com/public_html/" --user username:password

    Review directories and files before transfers.

  4. Upload a file to SFTP securely.
    $ curl --upload-file example.txt "sftp://sftp.example.com/home/user/" --user username:password

    SFTP uses SSH for encryption.

  5. Download a file from SFTP.
    $ curl --remote-name "sftp://sftp.example.com/home/user/example.txt" --user username:password

    Same syntax as FTP, just change protocol to sftp.

  6. List files on SFTP.
    $ curl "sftp://sftp.example.com/home/user/" --user username:password

    Ensure necessary permissions and keys are configured.

  7. Resume a partial FTP download with --continue-at -.
    $ curl --continue-at - --remote-name "ftp://ftp.example.com/public_html/example.txt" --user username:password

    Resume is possible if the server supports partial requests.

  8. Use a private key for SFTP.
    $ curl --upload-file example.txt "sftp://sftp.example.com/home/user/" --key ~/.ssh/id_rsa --pubkey ~/.ssh/id_rsa.pub --user username

    Key-based authentication avoids passwords.

Discuss the article:

Comment anonymously. Login not required.