Using cURL for FTP and SFTP transfers enables scripted uploads, downloads, and directory listings without interactive clients. Automating file movement in this way supports scheduled jobs, integration with build pipelines, and quick ad‑hoc transfers using a single, consistent command-line tool. Choosing between plain FTP and encrypted SFTP balances convenience and security based on the data and network environment.

The cURL command encodes the protocol, host, and path directly in the URL, such as ftp://ftp.example.com/public_html/ or sftp://sftp.example.com/home/user/. Options like --upload-file, --remote-name, and --continue-at control how files move between the local system and the remote server, while --user and key options supply credentials for authentication. The same syntax pattern applies across supported platforms, which keeps usage consistent on Linux, macOS, and Windows terminals.

Plain FTP exposes credentials and data in clear text, so it suits only non-sensitive content on trusted networks, whereas SFTP wraps traffic inside SSH with strong encryption. Firewalls, proxies, and server configuration can affect which protocol and ports are reachable, and large or unstable transfers often benefit from resume support rather than starting again from byte zero. Switching between FTP and SFTP typically involves changing only the URL scheme and authentication flags.

Steps to use cURL with FTP and SFTP:

  1. Open a terminal session in an environment where cURL can reach the target FTP or SFTP server.
  2. Upload a file to an FTP directory using basic authentication.
    $ curl --upload-file example.txt "ftp://ftp.example.com/public_html/" --user username:password
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                    Dload  Upload   Total   Spent    Left  Speed
    100    20    0     0  100    20      0   9037 --:--:-- --:--:-- --:--:-- 10000
    ##### snipped #####

    FTP transfers, including --user credentials and file contents, travel unencrypted and can be intercepted on untrusted networks.

  3. Download a file from an FTP directory while preserving the remote filename.
    $ curl --remote-name "ftp://ftp.example.com/public_html/example.txt" --user username:password
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                    Dload  Upload   Total   Spent    Left  Speed
    100    20  100    20    0     0   8485      0 --:--:-- --:--:-- --:--:-- 10000
    ##### snipped #####

    --remote-name stores the file using the server-side name instead of requiring a local filename argument.

  4. List the contents of an FTP directory to confirm paths before transferring files.
    $ curl --silent "ftp://ftp.example.com/public_html/" --user username:password
    -rw-r--r--   1 root     root           20 Dec 21 11:59 example.txt
    -rw-r--r--   1 root     root          157 Dec 21 09:32 project-2025-12-archive.tar.gz
    drwxr-xr-x   3 root     root         4096 Dec 21 09:33 releases
    -rw-r--r--   1 root     root       524288 Dec 21 11:43 report.tar.gz

    Directory listings reveal exact filenames, sizes, and timestamps, which helps avoid typos and accidental overwrites.

  5. Upload a file securely to an SFTP home directory using password authentication.
    $ curl --upload-file example.txt "sftp://sftp.example.com/example.txt" --user username:password
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                    Dload  Upload   Total   Spent    Left  Speed
    100    20    0     0  100    20      0     38 --:--:-- --:--:-- --:--:--    38
    ##### snipped #####

    SFTP uses the same username and password concept as SSH while encrypting both credentials and data in transit.

  6. Download a file from an SFTP directory using the same URL pattern as FTP.
    $ curl --remote-name "sftp://sftp.example.com/example.txt" --user username:password
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                    Dload  Upload   Total   Spent    Left  Speed
    100    20  100    20    0     0     35      0 --:--:-- --:--:-- --:--:--    35
    ##### snipped #####

    Reusing the same flags and layout across FTP and SFTP reduces mistakes when switching protocols.

  7. List files on an SFTP directory to inspect remote content and confirm transfer locations.
    $ curl --silent "sftp://sftp.example.com/" --user username:password
    drwx------    2 1001     100          4096 Dec 21 09:29 .ssh
    drwxr-xr-x    4 1001     100          4096 Dec 21 09:33 ..
    drwxr-xr-x    3 1001     100          4096 Dec 21 07:55 home
    -rw-r--r--    1 1001     100            20 Dec 21 12:01 example.txt
    drwxr-xr-x    4 1001     100          4096 Dec 21 09:33 .
    -rw-r--r--    1 1001     100           157 Dec 21 09:33 project-2025-12-archive.tar.gz

    Listing the directory after uploads confirms that files landed in the intended path with the expected ownership.

  8. Resume a partial FTP download instead of restarting from the beginning when supported by the server.
    $ curl --continue-at - --remote-name "ftp://ftp.example.com/public_html/report.tar.gz" --user username:password
    ** Resuming transfer from byte position 262144
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                    Dload  Upload   Total   Spent    Left  Speed
    100  256k  100  256k    0     0   132M      0 --:--:-- --:--:-- --:--:--  250M
    ##### snipped #####

    Resuming requires a matching partial local file; mismatched content produces corrupted downloads that often fail checksum or integrity tests.

  9. Upload a file over SFTP using existing SSH key material instead of a password.
    $ curl --upload-file example.txt "sftp://sftp.example.com/example.txt" --key ~/.ssh/id_rsa --pubkey ~/.ssh/id_rsa.pub --user username
    Enter host password for user 'username':
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                    Dload  Upload   Total   Spent    Left  Speed
    100    20    0     0  100    20      0     36 --:--:-- --:--:-- --:--:--    36
    ##### snipped #####

    Key-based authentication keeps passwords out of scripts and reuses the same OpenSSH keys employed for interactive SSH logins.

  10. Verify successful transfers by listing the remote SFTP directory again and checking for the expected filenames.
    $ curl --silent "sftp://sftp.example.com/" --user username:password
    drwx------    2 1001     100          4096 Dec 21 09:29 .ssh
    drwxr-xr-x    4 1001     100          4096 Dec 21 09:33 ..
    drwxr-xr-x    3 1001     100          4096 Dec 21 07:55 home
    -rw-r--r--    1 1001     100            20 Dec 21 12:01 example.txt
    drwxr-xr-x    4 1001     100          4096 Dec 21 09:33 .
    -rw-r--r--    1 1001     100           157 Dec 21 09:33 project-2025-12-archive.tar.gz

    Reliable success signals include completed progress meters without errors and the presence of transferred files in directory listings with the correct size.

Discuss the article:

Comment anonymously. Login not required.