sftp Command in Linux: Transfer Files Securely

By 

Updated on

13 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 entire directories when uploading and downloading. SFTP does not follow symbolic links during the traversal.
  • -a - Attempts to continue interrupted transfers instead of overwriting existing partial copies. Use it only when the existing partial contents match the source, or the resulting file may be corrupt.
  • -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

The lcd command changes the local working directory, which is where downloaded files are written. If you want everything you fetch to land in ~/Downloads, switch to it before running get:

Terminal
lcd ~/Downloads

When you need something the SFTP shell does not provide, prefix a command with ! to run it in your local shell. Typing ! on its own drops you into a local shell, and exit brings you back to the SFTP prompt:

Terminal
!tar -czf archive.tar.gz project/

The progress command toggles the transfer progress meter. Turning it off keeps the output readable when you are piping a session to a log file:

Terminal
progress

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 saved in the directory from which you typed the sftp command, or in whichever directory you last set with lcd.

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

The first line confirms the local source and the remote destination, and the second line reports the progress, the transferred size, the average speed, and the elapsed time. The file lands in the remote working directory, which is the remote user’s home directory unless you moved somewhere else with cd.

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

Terminal
put /home/local_username/backups/filename.zip

To store the file under a different name on the remote server, pass the new name as the second argument:

Terminal
put filename.zip archive-2026.zip

You can also send a file straight into a specific remote directory without changing the remote working directory first. End the path with a slash so SFTP treats it as a directory:

Terminal
put filename.zip /var/www/uploads/

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

Both get and put accept glob patterns, so you do not need a separate command to move several files at once. To download every .txt file from the remote working directory:

Terminal
get *.txt

Uploading works the same way:

Terminal
put *.log

If you are coming from ftp or from PuTTY’s psftp, you will probably reach for mget and mput. OpenSSH SFTP accepts both names, but they are undocumented aliases, so mget *.csv and get *.csv do exactly the same thing. Prefer the documented names, because the aliases appear neither in the man page nor in the help output.

There is also no prompt command to switch off. Wildcard transfers always run for every matching file without asking for confirmation, so check the pattern before you point it at a large directory.

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. This is what an SFTP chroot jail gives you: an account that can move files but cannot log in to a shell.

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

Transferring Files Without the Interactive Shell

You do not always need the SFTP prompt. When the destination includes a path that points to a file rather than a directory, sftp fetches it and exits:

Terminal
sftp remote_username@server_ip_or_hostname:/var/log/nginx/access.log

The file is saved in your current local directory under the same name. To save it somewhere else, or under a different name, add a local path as a second argument:

Terminal
sftp remote_username@server_ip_or_hostname:/var/log/nginx/access.log ~/logs/access-copy.log

With non-interactive public-key authentication and host verification already configured, the whole transfer runs unattended. With password authentication, sftp asks for the password first and then fetches the file.

If the path points to a directory, SFTP logs in and opens the interactive shell there instead of transferring anything. Add a trailing slash when you want to force that behavior for a name that could be read either way:

Terminal
sftp remote_username@server_ip_or_hostname:/var/www/

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 one file and exitsftp user@host:/path/file
Download one file to a local pathsftp user@host:/path/file ./local.txt
Download fileget filename
Download directoryget -r directory
Resume downloadreget filename
Resume directory downloadreget -r directory
Upload fileput filename
Upload under a different nameput filename newname
Upload into a remote directoryput filename /remote/dir/
Upload directoryput -r directory
Resume uploadreput filename
Resume directory uploadreput -r directory
Download multiple filesget *.txt
Upload multiple filesput *.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 a local shell command!command
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