Read in 5 minutes
How to Use Linux SFTP Command to Transfer Files
SFTP (SSH File Transfer Protocol) is a secure file protocol used to access, manage and transfer files over an encrypted SSH transport.
When compared with the traditional FTP protocol, SFTP offers all the functionality of FTP, and it is easier to configure.
scp command, which only allows file transfers, the
sftp command allows you to perform a range of operations on remote files and resume file transfers.
In this tutorial, we will show you how to use the Linux
Before you Begin
To be able to transfer and manage files via SFTP you must have write permission on the remote system.
The directory from where you run the
sftp command is the local working directory.
Establishing a SFTP connection
SFTP works on a client-server model. It is a subsystem of SSH and supports all SSH authentication mechanisms.
Although the traditional password authentication is set up by default and easer to use, if you regularly connect to your server via SSH/SFTP it is recommend to create SSH keys and setup a passwordless SFTP login.
To open a SFTP connection to a remote system use the
sftp command followed by the remote server username and IP address or domain name:
sftp [email protected]_ip_or_hostname
If you are using a password authentication you will be prompted to enter the user password. Once connected, the remote server will display a confirmation message and the
Connected to [email protected]_ip_or_hostname. sftp>
If the SSH server is not listening on the default port 22, use the
oPort option the specify the alternate port:
sftp -oPort=custom_port [email protected]_ip_or_hostname
Most of the SFTP commands are similar or identical to the commands you would use in Linux shell prompt.
You can get a list of all available SFTP commands by typing
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
Navigating with SFTP
Once you are logged in to remote server, your current working directory is the remote user home directory. You can check that by typing:
Remote working directory: /home/remote_username
To list the files and directories use the
To navigate to another directory, using the
cd command. For example to switch to the
/tmp directory you would type:
All of the above commands are used to navigate and work on the remote location. The
sftp prompt also provides commands for local navigation, information and file management.
For example to print the local working directory you would type:
Local working directory: /home/local_username
Transferring Files with SFTP
SFTP allows you to transfer files securely between two machines.
In most cases, you will use a desktop SFTP client like [WinSCP])(https://winscp.net/eng/index.php) or FileZila to connect to the remote server and download or upload files. However, the
sftp command is useful when you work on a server without GUI and you want to transfer files or when you want to perform other operations on the remote files.
Downloading Files with the SFTP Command
Once you are logged in to remote server, your current working directory is the remote user home directory.
When downloading files with the
sftp command, the files will be downloaded to the directory from which you typed the
To download a single file from the remote server, use the
The 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:13
If you want to save the downloaded file with a different name, specify the name afterwards:
get filename.zip local_filename.zip
To download a directory from the remote system use the recursive
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 same as the syntax of
Uploading Files with the SFTP Command
To upload a file from a local directory to a remote FTP server use the
The output should look something like this:
Uploading filename.zip to /home/remote_username/filename.zip filename.zip 100% 12MB 1.7MB/s 00:06
If you want upload a file that is not in your current working directory use the absolute path to the file.
When working with
put you can use same options that are available with the
To copy a local directory, you would type:
put -r locale_directory
To resume an interrupted upload:
File Manipulations with SFTP
Usually you would connect to your server via SSH and do you work using the shell terminal but in some cases your user may have only SFTP access 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:
Display statistics of the remote system disk usage:
Size Used Avail (root) %Capacity 20616252 1548776 18002580 19067476 7%
Create a new directory on the remote server:
Rename a file on the remote server:
rename file_name new_file_name
Delete a file on the remote server:
Delete a directory on the remote server:
Change the permissions of a file on the remote system:
chmod 644 file_name
Change the owner of a file on the remote system:
chown user_id file_name
You must supply the user ID to the
Change the group owner of a remote file with:
chgrp group_id file_name
Once you are done with your work you can close the connection by typing
In this tutorial, you learned how to use the sftp command to download and upload files to your remote SFTP server.
You may also want to setup a SSH key-based authentication and connect to your Linux servers without entering a password.
If you are regularly connecting to the same systems, you can simplify your workflow by defining all of your connections in the SSH config file.