How to Use SFTP to Transfer Files to/from Linux Servers

Most people prefer utilities like FileZilla or WinSCP when they need to transfer files to or from Linux/BSD servers. If the OpenSSH daemon is running on that computer, you can connect to it through the Secure/SSH FTP protocol (SFTP). The aforementioned utilities do make it easier to transfer files by offering a graphical interface, but you have to install and configure those programs.

Some purists prefer not having to install extra utilities. Other people simply prefer the command line because it gives you more control and a faster way to tell the utility what you want it to do. But now there’s an extra reason to resort to the command line when you want to transfer files. Windows 10 started to include, by default, an OpenSSH client that lets you log in to Linux/BSD servers directly from Command Prompt.

This client also comes with a bunch of other utilities, and SFTP is among them. So, you can now also transfer files to Linux/BSD operating systems, directly from Command Prompt, without having to install anything on your Windows machine.

The sftp utility is also available on Linux machines and works the same way.

How to Log in with the sftp Command

If you’re on Windows, open Command Prompt. If you’re on Linux, open a terminal window.

To log in to a server with the sftp command, you use the exact same syntax as with the ssh command. If you’re unfamiliar with this syntax, read the tutorial linked in the introduction paragraph.

Example commands:

sftp-logging-in

How to Download Files After You Log in with sftp

You can now enter commands at the sftp > prompt to interact with files on the server.

To download a file:

Example:

This will download to your current directory, the one you were in locally before you logged in to the server. To download to a specific local directory (on Windows):

Example

On Linux you simply use Unix type paths (forward slash instead of backslash):

Note: even if you (incorrectly) use forward slashes for Windows paths, in sftp, it seems the utility will understand them.

Paths can also be relative. This means that if you already were in C:\User\John when you opened Command Prompt, you can download to your desktop (C:\User\John\Desktop) with:

The remote paths can also be relative, which means that if you log in with sftp john@203.0.113.1, you will already be in the directory “/home/john,” so you can use this to download “/home/john/file:”

This would download “/home/john/file” to “C:\Users\YourUser\Desktop\file.”

To copy directories, you have to add the -r parameter to the command, which stands for recursive.

sftp-copy-directory

Remember to add a name for the new directory you want to create locally, like “Desktop\bin” in this case. If you would use get -r /bin Desktop, files would be copied directly on the Desktop. It’s the files that are copied, not the directory itself.

How to Upload Files After You Log in with sftp

Uploading files or directories follows the same principles. The only exception is that paths are reversed, meaning you first specify the local file/directory and then the remote path.

On Windows:

On Linux:

When uploading directories (recursive), remember that the same rule as in the previous section applies: it’s actually the files in the directory that are copied, not the directory itself. So specify a new name for a directory you want to copy those files to.

This would create a new directory called “bin” on the remote side.

To exit the sftp shell, simply type:

How to Resume Transfers and Use Paths that Contain Spaces

When you transfer a large file, if it gets interrupted, you can resume by replacing the previous command with reput and reget instead of put and get. Just make sure you use the exact same paths you used last time, so that source and destination match exactly.

To resume directory transfers, just add the -r parameter

If the path to a file contains spaces, put it within quotes.

This won’t work:

But this will:

(Yes, you can actually find the Witcher 3’s soundtrack there if you have it installed on Steam.)

Conclusion

This covers the most important things you have to know about sftp. If you find that you have a need for learning more, you can read the complete sftp command manual online.

2 comments

  1. Thank you for this tips !
    I usually use sshfs to mount a remote filesystem on local machine, but with an lxc container it’s impossible.
    Except if you bindmount the directory from local machine to the lxc container. (in the config file of the container)
    So to download a file or a directory and avoid mountpoints, this solution is very useful !

    Thank’s again ! ;-)

    jotux

    1. Thank you! Happy to know it helped.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.

Sponsored Stories