Home FTP How to Download and Upload Remote Directory in sFTP

How to Download and Upload Remote Directory in sFTP

sFTP or Secure File Transfer Protocol is a convenient upgrade from the traditional FTP (File Transfer Protocol). The sFTP program boasts of improved security layers since it mirrors encrypted SSH transport protocols in its file transfer operations.

sftp Command Usage
sftp Command Usage

The convenience of sFTP is in its flexible adaptation to useful SSH features like compression and public key authentication.

Once you successfully gain access to your remote machine or server via sFTP, this program takes its users to an interactive command line/terminal mode session where you can comfortably use its defined command syntax to execute the needed FTP-related operations.

This article seeks to demonstrate the security and convenience of sFTP in downloading a directory (folders and subfolders) from a remote machine/server.

sFTP and File Transfer

There are two sFTP approaches to securely connect to your remote machine via the SSH transport layer. One is through password authentication and the other through SSH passwordless login.

Going with SSH passwordless login is a recommended option since it is more secure and you won’t have to keep remembering your login password whenever you need access to your remote server.

Configuring Remote SSH Passwordless Access

1. Generate an SSH key on your local/current machine.

$ sudo ssh-keygen -t rsa

Skip the Enter passphrase: step by hitting [Enter] on your keyboard.

Configure Remote SSH Passwordless Access
Configure Remote SSH Passwordless Access

2. Next, copy generated SSH key to the remote machine.

$ sudo ssh-copy-id [email protected]
Copy SSH Key to Remote Machine
Copy SSH Key to Remote Machine

3. Test SSH passwordless access to your remote server.

$ sudo ssh [email protected]
Test Remote SSH Passwordless Access
Test Remote SSH Passwordless Access

SSH passwordless login was a success!

Attempting sFTP Passwordless Access

Log out from the server and attempt remote access via sFTP.

$ sudo sftp [email protected]
sFTP Passwordless Access
sFTP Passwordless Access

As expected, sFTP server access was a success. From the resulting sFTP console, you can execute common Linux terminal-based interactive commands like ls, pwd, lpwd, and mkdir.

sftp> ls
Run Commands in sFTP
Run Commands in sFTP

You should be able to navigate to & identify the targeted directory on the remote server which you wish to download. Since it is a directory, there is a high possibility of more folders and subfolders with files existing within it.

sftp> ls -l LinuxShellTips_Backup
List sFTP Directory
List sFTP Directory

Download a Linux Remote Directory Using sFTP

On my end, the targeted remote directory for download is highlighted as LinuxShellTips_Backup. To download this whole directory to my local machine, I will make use of the get command.

On its own, the get command is effective in downloading remote files and folders (without subfolders). Since we are dealing with a directory that might have multiple files and folders within folders, the get command needs the assistance of the -r (recursive) option to ensure all files, folders, and subfolders are downloaded.

sftp>  get -r LinuxShellTips_Backup   
Download Remote Directory Using sFTP
Download Remote Directory Using sFTP

The final step is to exit sFTP and check for the existence of the downloaded directory on the current working directory of the local machine you are using.

sftp> exit
$ ls -l LinuxShellTips_Backup  
Confirm Remote Directory Using-sFTP
Confirm Remote Directory Using-sFTP

Upload Directory to Remote Server Using sFTP

If you are curious about uploading a file to a remote server via sFTP, first make sure the remote server has the same directory name as the one you are about to upload.

If you are certain that the directory names are a match, use the put command together with the recursive option -r for successful remote directory upload.

sftp> put -r LinuxShellTips_Backup
Upload Directory to Remote Linux Using sFTP
Upload Directory to Remote Linux Using sFTP

You can now comfortably and securely download and upload directories via sFTP in Linux.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.