How to Upload or Download Files/Directories Using sFTP in Linux

sFTP (secure File Transfer Program) is a secure and interactive file transfer program, which works in a similar way as FTP (File Transfer Protocol). However, sFTP is more secure than FTP; it handles all operations over an encrypted SSH transport.

It can be configured to use several useful SSH features, such as public key authentication and compression. It connects and logs into the specified remote machine, and switches to an interactive command mode where user can execute various commands.

In this article, we will show you how to upload/download a whole directory (including it’s subdirectories and subfiles) using sFTP.

How to Use sFTP to Transfer Files/Directories in Linux

By default, SFTP adopts the same SSH transport for establishing a secure connection to a remote server. Although, passwords are used to authenticate users similar to the default SSH settings, but, it is recommended to create and use SSH passwordless login for simplified and more secure connection to remote hosts.

Connect to Remote FTP Server Securely

To connect to a remote sftp server, first establish an secure SSH connection and then create an SFTP session as shown.

$ sftp [email protected]

Once you have logged into the remote host, you can run interactive sFTP commands as in the examples below:

sftp> ls			#list directory 
sftp> pwd			#print working directory on remote host
sftp> lpwd			#print working directory on local host
sftp> mkdir uploads		#create a new directory
Run sFTP Commands on Remote Linux
Run sFTP Commands on Remote Linux

How to Upload a Directory Using sFTP

In order to upload a whole directory to a remote Linux host, use the put command. However, you will get an error if the directory name doesn’t exist in the working directory on the remote host as shown in the screenshot below.

Therefore, first create a directory with the same name on the remote host, before uploading it from the local host, the -r does the magic, enabling subdirectories and subfile to be copied as well:

sftp> put -r  Tecmint.com-articles
sftp> mkdir Tecmint.com-articles
sftp> put -r Tecmint.com-articles
Upload Directory using SFTP
Upload Directory using SFTP

To preserve the modification times, access times, and modes from the original files transferred, use the -p flag.

sftp> put -pr Tecmint.com-articles

How to Download a Directory Using sFTP

To download a whole directory called fstools-0.0 from remote Linux host to local machine, use the get command with the -r flag as follows:

sftp> get -r fstools-0.0
Download Directory using sFTP
Download Directory using sFTP

Then check in the current working directory on the local host, if the directory was downloaded with all the contents in it.

To quite the sFTP shell, type:

sftp> bye
OR
sftp> exit

Additionally, read through the sFTP commands and usage tips.

Note that to prevent users from accessing the whole file system on the remote host, for security reasons, you can restrict sFTP users to their home directories using chroot Jail.

That’s it! In this article, we’ve showed you how to upload/download a whole directory using sFTP. Use the comment section below to offer us your thoughts about this article/topic.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Aaron Kili
Aaron Kili
Contributor
About the author

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

3 Comments

Leave a Reply
  1. Hi is there a way to get an email notification once the user has been successfully uploaded/download a file to sftp directory?

    Thanks

    Reply
    • @ferjun

      Yes, it is possible, you need to configure an email server and write scripts to trigger a notification each time a user successfully uploads/downloads a file to/from the sftp directory.

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.