DEV Community

dpm_bush
dpm_bush

Posted on Originally published at sshflow.com

Download a Remote Directory Recursively with OpenSSH SFTP

Downloading one file with SFTP is straightforward. A directory is different: with OpenSSH’s interactive SFTP client, use the recursive flag on get to copy the directory and its contents.

sftp> get -R remote-directory
Enter fullscreen mode Exit fullscreen mode

That copies the directory tree from the server to your local machine. Before starting a large transfer, check which remote directory you are in and where local files will be saved. Those two locations are easy to mix up.

Download a directory

Connect to the server as usual:

sftp user@server
Enter fullscreen mode Exit fullscreen mode

At the sftp> prompt, run get -R with the remote directory path:

sftp> get -R /var/www/project
Enter fullscreen mode Exit fullscreen mode

For example, if the remote directory contains index.html, an assets folder, and an uploads folder, the recursive transfer includes those files and subdirectories.

A plain get is for retrieving files; it does not recursively copy a directory. For a directory tree, use the -R option on the interactive get command.

Choose where the files go locally

By default, SFTP writes downloaded files to its current local working directory. Check that location with lpwd:

sftp> lpwd
Enter fullscreen mode Exit fullscreen mode

To switch to a different local destination before downloading, use lcd:

sftp> lcd ~/Downloads
sftp> get -R /var/www/project
Enter fullscreen mode Exit fullscreen mode

You can also supply a local destination after the remote path:

sftp> get -R /var/www/project ~/Downloads/
Enter fullscreen mode Exit fullscreen mode

The local and remote navigation commands are separate:

  • cd changes the remote working directory.
  • pwd displays the remote working directory.
  • lcd changes the local working directory.
  • lpwd displays the local working directory.

If you want to use a relative remote path, first navigate to its parent and confirm your location:

sftp> cd /var/www
sftp> pwd
sftp> get -R project
Enter fullscreen mode Exit fullscreen mode

Checking pwd and lpwd before a large transfer helps avoid downloading the wrong directory or putting it somewhere unexpected.

Recursive flags: mind the context

OpenSSH uses similar-looking recursive flags in different places. At the interactive sftp> prompt, the directory download command is:

sftp> get -R remote-directory
Enter fullscreen mode Exit fullscreen mode

That is different from the -r option passed when starting the SFTP program. Also, uppercase -R on the program itself has a separate meaning: it controls the number of outstanding requests. If a flag is rejected, check the commands supported by the SFTP client you are using with help get; other clients or older versions may have different syntax.

There is another important behavior to know: OpenSSH does not follow symbolic links encountered during recursive traversal. If a directory contains a symlink to files elsewhere, do not assume those target files will be included in the download. Transfer the target separately if you need it.

Preserve file metadata

If you also want OpenSSH SFTP to preserve file permissions and access and modification times during the transfer, add -p:

sftp> get -pR /var/www/project
Enter fullscreen mode Exit fullscreen mode

This does not change what the recursive operation includes: it is still copying the directory tree. Whether you can read the remote files is governed by your account’s permissions and the server’s filesystem and SFTP configuration.

Download several directories

For a few directories, run one recursive get command for each:

sftp> get -R logs
sftp> get -R backups
sftp> get -R uploads
Enter fullscreen mode Exit fullscreen mode

If you only need matching files in the current remote directory, a glob can be simpler:

sftp> get *.log
Enter fullscreen mode Exit fullscreen mode

That retrieves matching files in the current directory; it is not a substitute for recursively copying a directory tree.

Make repeat transfers with batch mode

For a repeatable download, put the SFTP commands in a file such as download.sftp:

lcd /home/me/backups
get -R /var/www/project
bye
Enter fullscreen mode Exit fullscreen mode

Run the batch file with:

sftp -b download.sftp user@server
Enter fullscreen mode Exit fullscreen mode

Batch mode is useful for scripts and scheduled transfers, but authentication has to work without a password prompt. SSH keys are commonly used for non-interactive connections.

Troubleshooting checklist

If the download does not work as expected, check the location and path before changing the command:

  • Directory not copied: use get -R directory at the interactive SFTP prompt.
  • “No such file or directory”: run pwd and ls to check the remote location and directory name.
  • “Permission denied”: confirm that your account can read the directory and its files.
  • Files saved in the wrong place: check lpwd, or set the destination with lcd.
  • Symlink target missing: OpenSSH’s recursive traversal does not follow symbolic links; transfer the target separately if needed.

The basic pattern is simple: connect, check your remote and local locations, then run get -R with the directory you want. Use lcd to choose where it lands, and consider batch mode when the transfer needs to be repeated.

I originally published a more detailed version of this guide on the SSHFlow blog.

I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)