How to Use SFTP Commands to Transfer Files on Windows & Linux

  Use code KB4KDO0L9 to receive a 10% recurring discount on any server.

In this article, we will show you how to securely transfer files between your local machine and a remote machine, using SFTP (Secure File Transfer Protocol), also known as the SSH File Transfer Protocol.  The command line provides other alternative file transfer capabilities such as SCP, which also uses the SSH (secure shell) under the hood. In the examples, we will use a sample file netflix_titles.csv.zip hosted on our local machine, and we will securely transfer it using SFTP.

Log into Remote Server using SSH

SSH creates a secure connection between two systems. For this example, you would need a local and a remote system in place. If you don't have a remote system, quickly provision a remote server on Windows or Linux and log in using your root (user name) and password for authentication:

ssh root@172.105.186.216
root@172.105.186.216's password:
Last login: Fri Feb 26 14:28:52 2021 from 180.150.39.150
[root@sm2134-216 ~]#

Alternatively, you can generate SSH keys (public and private key pair) for authentication and log into the remote server using that. To ensure that no files exist on your remote server, check your current directory and list the content of the directory:

[root@sm2134-216 ~]# pwd
/root
[root@sm2134-216 ~]# ls

If you have provisioned a new server, there will be no files in the root directory. Exit the remote machine and hop back to your local machine and list the files and subdirectories in the current directory:

[root@sm2134-216 ~]# exit
░▒▓ ~/Projects/ServerMania ▓▒░─────────────────░▒▓ took 6m 1s ≡ at 12:10:39 ▓▒░

netflix_titles.csv.zip

The directory contains one file netflix_titles.csv.zip, which we will transfer to the remote machine using the SFTP command. In this example, we have used the root user, but please make sure that your user has the write permission on the remote server. Learn more about how to SSH on our blog.

Log into Remote Server using SFTP

SFTP or secure file transfer protocol is an upgrade from the traditional FTP (file transfer protocol). SFTP uses the SSH (secure shell) for securing the connection.

Let us login to the remote machine using the SFTP command and start an SFTP session and run the ? or help command:

❯ sftp root@172.105.186.216
root@172.105.186.216's password:
Connected to 172.105.186.216.
sftp> ?

This will list all the possible SFTP commands, but we want to look at only a couple of them. Most of these commands resemble basic shell commands for navigation, file and directory creation, etc. Let's look at some of the examples of SFTP commands:

  1. sftp> put - Upload file
  2. sftp> get - Download file
  3. sftp> cd path - Change remote directory to 'path'
  4. sftp> pwd - Display remote working directory
  5. sftp> lcd path - Change the local directory to 'path'
  6. sftp> lpwd - Display local working directory
  7. sftp> ls - Display the contents of the remote working directory
  8. sftp> lls - Display the contents of the local working directory


In the following code snippet, you can see the examples of some of the commands mentioned above run on the SFTP prompt:

░▒▓ ~/Projects/ServerMania ▓▒░──────────────────────────────░▒▓ ≡  at 20:51:54 ▓▒░
❯ sftp root@172.105.186.216
root@172.105.186.216's password:
Connected to 172.105.186.216.
sftp> pwd
Remote working directory: /root
sftp> ls
sftp> lpwd
Local working directory: /Users/kovid/Projects/ServerMania
sftp>
sftp> mkdir files
sftp> cd files
sftp> pwd
Remote working directory: /root/files
sftp> cd ..
sftp> rmdir files
sftp>


Now that you know how to navigate the file system of both the local machine and the remote server, you will learn how to transfer files from one to another.

Transfer Files from Local Machine to Remote Server

First, let us see how a file can be transferred from a local machine to a remote machine using the secure file transfer protocol. Log in to the server to access the SFTP prompt and navigate to the local directory, which has the file to be transferred:

Connected to 172.105.186.216.
sftp> lpwd
Local working directory: /Users/kovid
sftp> lcd /Users/kovid/Projects/ServerMania
sftp> lpwd
Local working directory: /Users/kovid/Projects/ServerMania
sftp> lls
netflix_titles.csv.zip
sftp> put netflix_titles.csv.zip
Uploading netflix_titles.csv.zip to /root/netflix_titles.csv.zip
netflix_titles.csv.zip 100% 1207KB 1.5MB/s 00:00
sftp>

As you can see, using the put command, we have successfully transferred the file netflix_titles.csv.zip from our local machine to the remote machine. Notice that we did not provide any path on the remote machine, so the file got copied in the remote machine's current directory. Verify if the file has been copied or not by running the ls command:

sftp> pwd
Remote working directory: /root
sftp> ls
netflix_titles.csv.zip
sftp> bye

Use the bye command to close the connection (SFTP session).

Transfer Files from Remote Server to Local Machine

Now, let us delete the local file from our local machine and copy it back from the remote server fetching the remote file using the get command:

❯ pwd
/Users/kovid/Projects/ServerMania
❯ ls
netflix_titles.csv.zip
░▒▓ ~/Projects/ServerMania ▓▒░──────────────────────────░▒▓ ≡ at 21:16:22 ▓▒░
❯ rm netflix_titles.csv.zip
░▒▓ ~/Projects/ServerMania ▓▒░──────────────────────────░▒▓ ≡ at 21:16:32 ▓▒░
❯ ls
░▒▓ ~/Projects/ServerMania ▓▒░──────────────────────────░▒▓ ≡ at 21:16:34 ▓▒░

After removing the file from the local system, establish an SFTP connection with the remote machine again:

❯ sftp root@172.105.186.216
root@172.105.186.216's password:
Connected to 172.105.186.216.
sftp> get netflix_titles.csv.zip
Fetching /root/netflix_titles.csv.zip to netflix_titles.csv.zip
/root/netflix_titles.csv.zip 100% 1207KB 4.8MB/s 00:00
sftp>
sftp> lpwd
Local working directory: /Users/kovid/Projects/ServerMania
sftp> lls
netflix_titles.csv.zip
sftp> bye

Alternatively, you can also copy the remote file to your local system using the SFTP command without establishing a persistent connection from your local command line. This doesn't have to use the get command. Look at the following command:

░▒▓ ~/Projects/ServerMania ▓▒░──────────────────────────░▒▓ ≡  at 21:25:12 ▓▒░
❯ sftp root@172.105.186.216:netflix_titles.csv.zip .
root@172.105.186.216's password:
Connected to 172.105.186.216.
Fetching /root/netflix_titles.csv.zip to ./netflix_titles.csv.zip
/root/netflix_titles.csv.zip 100% 1207KB 4.8MB/s 00:00
░▒▓ ~/Projects/ServerMania ▓▒░─────────────────░▒▓ took 4s ≡ at 21:25:22 ▓▒░

Next Steps

The SFTP command is an easy and secure method to transfer files directly between two servers. Now you have all that is required to use this command with ease!

Still looking for some help getting all this running on your ServerMania servers? Contact our support team, or share your feedback in the comments below!

Complete Digital Server Solutions For All

Bare Metal

Dedicated Servers

A single tenant, physical server allowing you full access to its resources.

Professional-grade

Hybrid Servers

Virtualized server platform hosted on enterprise-grade physical servers.

Affordable & Secure

Cloud Servers

High-performance and highly-available infrastructure, delivering a reliable, secure, and scalable solution.