How to Use SFTP Command to Transfer Files

By 

Updated on

10 min read

Linux SFTP Command

When you need to move files to or from a remote server over SSH, SFTP (SSH File Transfer Protocol) gives you an interactive file transfer shell. You can browse remote directories, upload and download files, resume interrupted transfers, and manage files without opening a full SSH shell.

Compared to FTP , SFTP provides the same core functionality while being significantly more secure and easier to configure.

Unlike SCP , which supports only file transfers, SFTP supports a full range of file operations on remote files, including resuming interrupted transfers.

This guide shows you how to use the sftp command on Linux with practical examples for everyday file transfers.

Prerequisites

To upload or modify files via SFTP, you need write permission on the remote system.

For large transfers, it is recommended to run the sftp command inside a screen or tmux session to prevent interruptions.

The directory from which you run the sftp command becomes your local working directory.

Info
Do not confuse SFTP with FTPS. Both protocols serve the same purpose. However, FTPS stands for FTP Secure, and it is an extension to the standard FTP protocol that adds TLS/SSL encryption.

SFTP Command Syntax

The basic syntax of the sftp command is:

txt
sftp [OPTIONS] [user@]host[:path]

You can also use an SFTP URI:

txt
sftp://[user@]host[:port][/path]

The user value is the remote username, and host is the server hostname or IP address. If you add a path after the host, SFTP opens that remote directory after login. When the path points to a file and non-interactive authentication is available, SFTP can retrieve the file directly.

Connecting to a Remote Server

SFTP works on a client-server model. It is a subsystem of SSH and supports all SSH authentication methods.

To connect to a remote system, run the sftp command followed by the remote server username and the IP address or domain name:

Terminal
sftp remote_username@server_ip_or_hostname

If you are connecting to the host using password authentication, you will be prompted to enter the user password.

Once authenticated, you will be presented with the sftp prompt, and you can start interacting with the remote server:

output
Connected to remote_username@server_ip_or_hostname.
sftp>

If the remote SSH server listens on a non-standard port (for example, 2222), use the -P option to specify the port:

Terminal
sftp -P 2222 remote_username@server_ip_or_hostname

Common SFTP Options

The sftp command accepts several options that modify its behavior. Here are the most commonly used ones:

  • -P port - Specifies the port to connect to on the remote host.
  • -i identity_file - Selects the private key file for public key authentication.
  • -o ssh_option - Passes options to SSH in the format used in the SSH config file .
  • -C - Enables compression, which can speed up transfers over slow connections.
  • -v - Prints debugging messages about the connection and authentication process.
  • -b batchfile - Reads SFTP commands from a file instead of the terminal.
  • -p - Preserves modification times, access times, and file modes during transfers.
  • -r - Recursively copies directories when the command-line destination includes a path.
  • -l limit - Limits bandwidth in Kbit/s.
  • -J destination - Connects through a jump host.

For example, to connect with verbose output and compression enabled:

Terminal
sftp -v -C remote_username@server_ip_or_hostname

Basic SFTP Commands

Most SFTP commands are similar or identical to the Linux shell commands.

To get a list of all available SFTP commands, type help, or ?.

Terminal
help

The output will include a long list of all available commands, including a short description of each command:

output
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
...
...
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help

When you are logged in to the remote server, your current working directory is the remote user’s home directory. You can check that by typing:

Terminal
pwd
output
Remote working directory: /home/remote_username

To list the remote files and directories, use the ls command:

Terminal
ls

To navigate to another directory, use the cd command. For example, to switch to the /tmp directory, you would type:

Terminal
cd /tmp

The above commands are used to navigate and work on the remote location.

The SFTP shell also provides commands for local navigation, information, and file management. The local commands are prefixed with the letter l.

For example, to print the local working directory, you would type:

Terminal
lpwd
output
Local working directory: /home/local_username

To list local files:

Terminal
lls

Downloading Files

To download a single file from the remote server, use the get command:

Terminal
get filename.zip

The output should look something like this:

output
Fetching /home/remote_username/filename.zip to filename.zip
/home/remote_username/filename.zip                           100%   24MB   1.8MB/s   00:13

When downloading files with sftp, the files are downloaded to the directory from which you typed the sftp command.

If you want to save the downloaded file with a different name, specify the new name as the second argument:

Terminal
get filename.zip local_filename.zip

To download a directory from the remote system, use the recursive -r option:

Terminal
get -r remote_directory

If a file transfer fails or is interrupted, you can resume it using the reget command.

The syntax of reget is the same as the syntax of get:

Terminal
reget filename.zip

To resume a recursive directory download, add -r:

Terminal
reget -r remote_directory

Uploading Files

To upload a file from the local machine to the remote SFTP server, use the put command:

Terminal
put filename.zip

The output should look something like this:

output
Uploading filename.zip to /home/remote_username/filename.zip
filename.zip                          100%   12MB   1.7MB/s   00:06

If the file you want to upload is not located in your current working directory, use the absolute path to the file.

When working with put, you can use the same options that are available with the get command.

To upload a local directory, use -r:

Terminal
put -r local_directory

To resume an interrupted upload:

Terminal
reput filename.zip

To resume a recursive directory upload, run:

Terminal
reput -r local_directory

Transferring Multiple Files

You can use wildcard patterns to transfer multiple files at once with the mget and mput commands.

To download multiple files matching a pattern:

Terminal
mget *.txt

To upload multiple files:

Terminal
mput *.log

You do not need a separate prompt off command for mget or mput in OpenSSH SFTP. Wildcard transfers run for all matching files:

Terminal
mget *.csv

File Manipulations with SFTP

Typically, to perform tasks on a remote server, you would connect via SSH and run your commands in the shell. However, in some situations, the user may have only SFTP access (no full SSH shell) to the remote server.

SFTP allows you to perform some basic file manipulation commands. Below are some examples of how to use the SFTP shell:

  • Get information about the remote system’s disk usage :

    Terminal
    df
    output
            Size         Used        Avail       (root)    %Capacity
        20616252      1548776     18002580     19067476           7%
  • Create a new directory on the remote server:

    Terminal
    mkdir directory_name
  • Rename a file on the remote server:

    Terminal
    rename file_name new_file_name
  • Delete a file on the remote server:

    Terminal
    rm file_name
  • Delete a directory on the remote server:

    Terminal
    rmdir directory_name
  • Change the permissions of a file on the remote system:

    Terminal
    chmod 644 file_name
  • Change the owner of a file on the remote system:

    Terminal
    chown user_id file_name

    You must supply the user ID to the chown and chgrp commands.

  • Change the group owner of a remote file with:

    Terminal
    chgrp group_id file_name

Batch Mode

You can use batch mode to automate file transfers in scripts. Create a file containing SFTP commands, one per line, then execute it with the -b option.

In the following example, we create a batch file with commands to download files:

sftp_commands.txttxt
cd /var/www
get database_backup.sql
get config.php
bye

Run the batch file:

Terminal
sftp -b sftp_commands.txt remote_username@server_ip_or_hostname

For batch mode to work without manual intervention, you must use SSH key-based authentication .

If one of the batch commands fails, SFTP terminates immediately. To continue after a specific command fails, prefix that command with - inside the batch file:

sftp_commands.txttxt
cd /var/www
-get optional-report.csv
get database_backup.sql
bye

In this example, SFTP continues if optional-report.csv is missing, but it still stops if cd /var/www or get database_backup.sql fails.

Disconnecting

Once you are done with your work, close the connection by typing bye or quit. Both commands end the session:

Terminal
bye

Tips for Frequent Use

  • Set up an SSH key-based authentication and connect to your Linux servers without entering a password.

  • If you regularly connect to the same hosts, simplify your workflow by defining all of your connections in the SSH config file .

    ~/.ssh/configssh
    Host myserver_name
        HostName 10.10.0.2
        User leah
        Port 2222

Quick Reference

For a printable quick reference, see the SFTP cheatsheet .

TaskCommand
Connect to serversftp user@host
Connect on custom portsftp -P 2222 user@host
Download fileget filename
Download directoryget -r directory
Resume downloadreget filename
Resume directory downloadreget -r directory
Upload fileput filename
Upload directoryput -r directory
Resume uploadreput filename
Resume directory uploadreput -r directory
Download multiple filesmget *.txt
Upload multiple filesmput *.log
List remote filesls
List local fileslls
Change remote directorycd /path
Change local directorylcd /path
Print remote directorypwd
Print local directorylpwd
Create remote directorymkdir dirname
Delete remote filerm filename
Delete remote directoryrmdir dirname
Rename remote filerename oldname newname
Change permissionschmod 644 filename
Run batch filesftp -b sftp_commands.txt user@host
Disconnectbye or quit

Troubleshooting

Connection refused
The SSH server may not be running, or a firewall is blocking port 22 (or your custom port). Verify the server is accessible and the SSH service is running.

Permission denied (publickey)
Your SSH key is not authorized on the remote server, or you are using the wrong key. Use -i /path/to/key to specify the correct identity file, or check that your public key is in the remote user’s ~/.ssh/authorized_keys.

No such file or directory
The file or directory you are trying to access does not exist. Use ls to verify the path and check for typos.

Permission denied when uploading
You do not have write permission to the destination directory. Contact the server administrator or choose a directory where you have write access.

Connection timed out
Network issues or firewall rules are preventing the connection. Check your network connection and verify the server IP address is correct.

Broken pipe or connection reset
The connection was interrupted, often due to network instability or server-side timeouts. Use reget or reput to resume interrupted transfers.

FAQ

What is the default SFTP port?
SFTP uses port 22 by default, the same as SSH. You can specify a different port with the -P option.

What is the difference between SFTP and SCP?
Both use SSH for secure transfers. SCP is simpler and only transfers files, while SFTP provides an interactive shell with directory navigation, file manipulation, and the ability to resume interrupted transfers.

What is the difference between SFTP and FTPS?
SFTP runs over SSH (port 22), while FTPS is FTP with TLS/SSL encryption (typically port 990 or 21). SFTP is generally easier to configure since it uses a single port.

Can I use SFTP without a password?
Yes, by setting up SSH key-based authentication . This is also required for batch mode automation.

How do I transfer an entire directory?
Use the -r (recursive) option with get or put inside the SFTP prompt. For example: get -r remote_directory or put -r local_directory.

How do I resume a failed transfer?
Use reget to resume downloads and reput to resume uploads. For recursive directory transfers, use reget -r or reput -r.

Conclusion

The sftp command is useful when you need secure, interactive file transfers over SSH. For one-time copies, scp may be faster to type, but SFTP is a better fit when you need to browse directories, resume transfers, or automate a set of file operations with batch mode.

If you are working on a desktop machine, you can use a GUI SFTP client like WinSCP or FileZilla to connect to the remote server and download or upload files.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page