Using sftp -r

sftp -r is used for downloading or uploading entire directories recursively. Learn how to use it in this post.
  1. Blog

While using SFTP on the command line, there might be instances when you'll want to upload or download an entire directory and all the the files and subdirectories underneath it. To accomplish that, you'll need the recursive argument -r.

So, for example, you want to download the directory 'subfolder1' and all its contents from your SFTP server. To do that, just login to your SFTP server, navigate to the parent directory of the directory named "subfolder", and then issue this command:

 get -r subfolder1

That command will download subfolder1 and all its files and subdirectories into the current local directory. So if you're in your Home directory, that command should download everything into Home.

Note that the directory structure will be preserved.

If, instead of downloading, you would like to upload a directory (named subfolder1) recursively unto your server, you may run this command:

 put -r subfolder1

sftp_put_-r

Just like the previous command, that will store the uploaded directory and all its contents unto the current directory on the server side.

To upload files into a specific directory/subdirectory, you can either navigate to the destination directory before issuing the 'put' command or specify the destination like this:

put -r subfolder1 /enter/path/of/destination/directory

Now that you know how to use the sftp -r argument, you might want to broaden your knowledge about the SFTP command line. Here are some posts to help you down that path.

Using SFTP On The Command Line

Setting Up SFTP Public Key Authentication On The Command Line