How to Use SFTP Command to Transfer Files

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.
SFTP Command Syntax
The basic syntax of the sftp command is:
sftp [OPTIONS] [user@]host[:path]You can also use an SFTP URI:
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:
sftp remote_username@server_ip_or_hostnameIf 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:
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:
sftp -P 2222 remote_username@server_ip_or_hostnameCommon 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:
sftp -v -C remote_username@server_ip_or_hostnameBasic 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 ?.
helpThe output will include a long list of all available commands, including a short description of each command:
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 helpNavigating Directories with SFTP
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:
pwdRemote working directory: /home/remote_usernameTo list the remote files and directories, use the ls command:
lsTo navigate to another directory, use the cd command. For example, to switch to the /tmp directory, you would type:
cd /tmpThe 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:
lpwdLocal working directory: /home/local_usernameTo list local files:
llsDownloading Files
To download a single file from the remote server, use the get command:
get filename.zipThe output should look something like this:
Fetching /home/remote_username/filename.zip to filename.zip
/home/remote_username/filename.zip 100% 24MB 1.8MB/s 00:13When 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:
get filename.zip local_filename.zipTo download a directory from the remote system, use the recursive -r option:
get -r remote_directoryIf 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:
reget filename.zipTo resume a recursive directory download, add -r:
reget -r remote_directoryUploading Files
To upload a file from the local machine to the remote SFTP server, use the put command:
put filename.zipThe output should look something like this:
Uploading filename.zip to /home/remote_username/filename.zip
filename.zip 100% 12MB 1.7MB/s 00:06If 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:
put -r local_directoryTo resume an interrupted upload:
reput filename.zipTo resume a recursive directory upload, run:
reput -r local_directoryTransferring 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:
mget *.txtTo upload multiple files:
mput *.logYou do not need a separate prompt off command for mget or mput in OpenSSH SFTP. Wildcard transfers run for all matching files:
mget *.csvFile 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 :
TerminaldfoutputSize Used Avail (root) %Capacity 20616252 1548776 18002580 19067476 7%Create a new directory on the remote server:
Terminalmkdir directory_nameRename a file on the remote server:
Terminalrename file_name new_file_nameDelete a file on the remote server:
Terminalrm file_nameDelete a directory on the remote server:
Terminalrmdir directory_nameChange the permissions of a file on the remote system:
Terminalchmod 644 file_nameChange the owner of a file on the remote system:
Terminalchown user_id file_nameYou must supply the user ID to the
chownandchgrpcommands.Change the group owner of a remote file with:
Terminalchgrp 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:
cd /var/www
get database_backup.sql
get config.php
byeRun the batch file:
sftp -b sftp_commands.txt remote_username@server_ip_or_hostnameFor 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:
cd /var/www
-get optional-report.csv
get database_backup.sql
byeIn 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:
byeTips 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/configsshHost myserver_name HostName 10.10.0.2 User leah Port 2222
Quick Reference
For a printable quick reference, see the SFTP cheatsheet .
| Task | Command |
|---|---|
| Connect to server | sftp user@host |
| Connect on custom port | sftp -P 2222 user@host |
| Download file | get filename |
| Download directory | get -r directory |
| Resume download | reget filename |
| Resume directory download | reget -r directory |
| Upload file | put filename |
| Upload directory | put -r directory |
| Resume upload | reput filename |
| Resume directory upload | reput -r directory |
| Download multiple files | mget *.txt |
| Upload multiple files | mput *.log |
| List remote files | ls |
| List local files | lls |
| Change remote directory | cd /path |
| Change local directory | lcd /path |
| Print remote directory | pwd |
| Print local directory | lpwd |
| Create remote directory | mkdir dirname |
| Delete remote file | rm filename |
| Delete remote directory | rmdir dirname |
| Rename remote file | rename oldname newname |
| Change permissions | chmod 644 filename |
| Run batch file | sftp -b sftp_commands.txt user@host |
| Disconnect | bye 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.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

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