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.
sftp your_username@IP-address-or-hostname
Example commands:
sftp john@203.0.113.1 sftp john@example.com
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:
get /path/to/file/on/server
Example:
get /bin/ls
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):
get /path/to/file/on/server C:\path\to\local\folder
Example
get /bin/ls C:\User\John\Desktop
On Linux you simply use Unix type paths (forward slash instead of backslash):
get /bin/ls /home/john/Desktop
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:
get /bin/ls Desktop
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:”
get file Desktop
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.
get -r /bin Desktop\bin
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:
put C:\path\to\local\content /path/where/to/upload
On Linux:
put /path/to/local/content /path/to/remote/location
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.
put -r Desktop/bin bin
This would create a new directory called “bin” on the remote side.
To exit the sftp shell, simply type:
exit
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.
reget /path/to/file/on/server C:\path\to\local\file reput C:\path\to\local\file /path/to/file/on/server
To resume directory transfers, just add the -r
parameter
reput -r Desktop/bin bin
If the path to a file contains spaces, put it within quotes.
This won’t work:
put C:\Program Files (x86)\Steam\steamapps\common\The Witcher 3\SOUNDTRACK\The_Witcher_3_Wild_Hunt_-_Official_Soundtrack_(steam_edition)_mp3.zip
But this will:
put "C:\Program Files (x86)\Steam\steamapps\common\The Witcher 3\SOUNDTRACK\The_Witcher_3_Wild_Hunt_-_Official_Soundtrack_(steam_edition)_mp3.zip"
(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.
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
Thank you! Happy to know it helped.