Sep 20, 2023 5 min read

How to Use SFTP Command to Transfer Files

Use SFTP Command to Transfer Files with our step-by-step tutorial. SFTP is a secure file protocol for accessing, managing and transferring files.

Use SFTP Command to Transfer Files
Table of Contents

Introduction

SSH File Transfer Protocol (SFTP) is a secure file protocol for accessing, managing and transferring files through an encrypted SSH connection.

When compared to the traditional FTP protocol, SFTP provides all the same features as FTP while being more secure and easier to set up.

Unlike SCP, which simply allows you to transfer files, SFTP allows you to execute a variety of functions on distant files as well as continue file transfers.

In this tutorial, we'll show you how to use the Linux sftp command. We will also address a few FAQs on how to use sftp command to transfer files.

Before you Begin

You must have written permission on the remote machine to transmit files over SFTP.

It's best to use the sftp command inside a screen or tmux session when transferring huge files.

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

👉
SFTP should not be confused with FTPS. Both protocols have the same goal in mind. FTPS, on the other hand, stands for FTP Secure and is an enhancement of the basic FTP protocol that includes TLS capability.

Establishing an SFTP connection

SFTP is a client-server protocol. It's an SSH subsystem that supports all of SSH's authentication methods.

Use the sftp command, followed by the remote server username and the IP address or domain name, to establish an SFTP connection to a remote system:

sftp remote_username@server_ip_or_hostname

You will be required to enter the user password if you are connecting to the host using password authentication.

You'll be provided with the sftp prompt once you've connected, and you may begin communicating with the remote server:

Connected to remote_username@server_ip_or_hostname.
sftp>

Use the -P option to specify the SFTP port if the remote SSH server is not listening on the default port 22:

sftp -P custom_port remote_username@server_ip_or_hostname

SFTP Commands

The majority of SFTP commands are similar to or identical to those found in the Linux shell.

Type help or ? to see a list of all available SFTP commands.

help

This will generate a long list of all accessible commands, along with a brief description for each:

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

The remote user home directory is your current working directory when you log in to the remote server. You can double-check by typing:

pwd
Output

Remote working directory: /home/remote_username

Use the ls command to see a list of files and directories:

ls

Use the cd command to move to another directory. To get to the /tmp directory, for example, type:

cd /tmp

To navigate and work on the remote location, use the above commands.

Local navigation, information, and file management are also available through the SFTP shell. The letter l is used to denote local commands.

To print the local working directory, for example, you might type:

cd lpwd
Output

Local working directory: /home/local_username

Transferring Files with SFTP

You can use SFTP to securely transmit files between two machines.

If you're working on a desktop computer, you can connect to the distant server and download or upload files using a GUI SFTP client like WinSCP or FileZilla.

When working on a server without a GUI and need to transfer files or do other actions on distant files, the sftp command comes in handy.

Download Files with the SFTP Command

Use the get command to download a single file from a remote server:

get filename.zip

You will get an output like below:

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

When you use sftp to download files, the files are saved in the directory where you typed the sftp command.

If you want to save the downloaded file under a different name, use the second parameter to specify the new name:

get filename.zip local_filename.zip

Use the recursive -r option to download a directory from a remote system:

get -r remote_directory

If a file transfer fails or is halted, use the reget command to continue it.

The syntax of reget is identical to that of get:

reget filename.zip

Uploading Files with the SFTP Command

Use the put command to upload a file from your local workstation to a distant SFTP server:

put filename.zip

This is what the final product should look like:

Output

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

Use the absolute path of the file if the file you wish to upload isn't in your current working directory.

You can use the same options that you can with the get command when working with put.

Type the below command, to upload to a local directory:

put -r locale_directory

To resume an interrupted upload, follow these steps:

reput filename.zip

Manipulation of Files through SFTP

To complete duties on a remote server, you would typically connect to it using SSH and work in the shell terminal. In rare cases, however, the user may only have SFTP access to the distant server.

You may use SFTP to conduct some simple file manipulation operations. Some examples of how to utilize the SFTP shell are given below:

  • Obtain information about the disc use of the remote system:
df
Output

Size         Used        Avail       (root)    %Capacity
    20616252      1548776     18002580     19067476           7%
  • On the remote server, create a new directory:
mkdir directory_name
  • On the remote server, rename a file:
rename file_name new_file_name
  • To remove a file from a remote server, follow these steps:
rm file_name
  • On the remote server, delete a directory:
rmdir directory_name
  • Change a file's permissions on a remote system:
chmod 644 file_name
  • On the remote system, change the owner of a file:
chown user_id file_name

The user ID must be supplied to the chown and chgrp commands.

  • Change the owner of a remote file's group using:
chgrp group_id file_name

When you've finished your work, type bye or quit to close the connection.

FAQs to Use SFTP Command to Transfer Files

What is the difference between FTP and SFTP? 

FTP (File Transfer Protocol) is an insecure protocol, while SFTP (SSH File Transfer Protocol) is a secure version of FTP that provides encryption and authentication, making it a safer option for transferring files.

How do I specify a non-standard port for SFTP? 

To connect to an SFTP server on a non-standard port, use the -P option followed by the desired port number. For example, sftp -P 2222 username@hostname.

Can I transfer multiple files at once using the SFTP command? 

Yes, you can transfer multiple files by providing multiple filenames separated by spaces after the put or get command. For example, put file1.txt file2.txt will upload both files to the remote server.

How can I transfer entire directories using the SFTP command? 

Use the put -r command to upload an entire directory and its contents. Similarly, get -r command can be used to download a directory from the remote server.

Can I resume interrupted file transfers with SFTP? 

Yes, SFTP supports resuming interrupted transfers. By default, if a partially transferred file with the same name exists on the destination, SFTP will resume the transfer instead of starting from scratch.

Can I delete files using the SFTP command? 

Yes, you can delete files from the remote server using the rm command. For example, rm file.txt will delete the specified file.

How can I change the local or remote working directory? 

To change the local working directory, use the lcd command followed by the desired directory path. To change the remote working directory, use the cd command followed by the desired directory path.

Conclusion

We've taught you how to use the sftp command to download and upload files to your remote SFTP server in this tutorial.

Set up SSH key-based authentication to log in to your Linux servers without having to enter a password. If you connect to the same servers on a frequent basis, you may make your process easier by defining all of your connections in the SSH config file.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.