How to transfer files to / from Linux servers using SFTP

If the OpenSSH daemon is running on that computer, users can connect to it via Secure / SSH FTP (SFTP) protocol.

Most people prefer to use 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, users can connect to it via Secure / SSH FTP (SFTP) protocol. The aforementioned utilities make file transfer easier by providing a graphical interface, but users must install and configure those programs.

Some simpleists don't want to install additional utilities. Others simply like the command line because it allows more control and faster to 'say' with what utilities the user wants it to do. But now, there's another reason to use the command line when you want to transfer files. By default, Windows 10 starts to include an OpenSSH client application that allows logging in to Linux / BSD servers directly from the Command Prompt.

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

Instructions for using SFTP to transfer files to / from Linux servers

  1. How to login using the sftp command
  2. How to download the file after logging in with sftp
  3. How to upload files after logging in with sftp
  4. How to continue to transfer and use spaces containing spaces

How to login using the sftp command

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

To log in to the server using the sftp command, use the same syntax as the ssh command.

 sftp your_username@IP-address-or-hostname 

Example commands:

 sftp john@203.0.113.1 sftp john@example.com 

How to transfer files to / from Linux servers using SFTP Picture 1How to transfer files to / from Linux servers using SFTP Picture 1

How to download the file after logging in with sftp

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

To download a file:

 get /path/to/file/on/server 

For example:

 get /bin/ls 

This command will download the current directory (the directory before logging in to the server). To download a specific local directory (on Windows):

 get /path/to/file/on/server C:pathtolocalfolder 

For example:

 get /bin/ls C:UserJohnDesktop 

On Linux, just use Unix type paths (forward slashes instead of backslashes):

 get /bin/ls /home/john/Desktop 

Note : Even when using slashes (incorrect) for Windows paths in sftp, it seems that the utility will still understand them.

The path may also be relative. This means that if it's already in C: UserJohn when you open the Command Prompt, the user can download the desktop (C: UserJohnDesktop) with:

 get /bin/ls Desktop 

Remote paths can also be relative. This means that if you log in with sftp john@203.0.113.1, the user is already in the '/ home / john' directory, so you can use it to download '/ home / john / file'.

 get file Desktop 

This command will download '/ home / john / file' into 'C: UsersYourUserDesktopfile'.

How to transfer files to / from Linux servers using SFTP Picture 2How to transfer files to / from Linux servers using SFTP Picture 2

To copy the directory, the user must add the -r parameter to the command.

 get -r /bin Desktopbin 

Remember to add a name for the new folder you want to create locally, as in 'Desktopbin' in this case. If using get -r / bin Desktop , the files will be copied directly on the Desktop. It requires files to be copied, not the directory itself.

How to upload files after logging in with sftp

Upload to files or directories according to the same rules. The only exception is that the paths are reversed, meaning that the user first specifies the local file / directory and then the remote path.

On Windows:

 put C:pathtolocalcontent /path/where/to/upload 

On Linux:

 put /path/to/local/content /path/to/remote/location 

When uploading folders, remember that the same rules as in the previous section apply. It is actually the files in the folder that are copied, not the folder itself. Therefore, specify a new name for a folder you want to copy those files into.

 put -r Desktop/bin bin 

This will create a new folder named 'bin'.

To exit shell sftp, just type:

 exit 

How to continue to transfer and use spaces containing spaces

When transferring a large file, if interrupted, the user can continue by replacing the previous command with reput and reget (instead of put and get). Just make sure you use the exact paths you used before, leaving the source and destination exactly matched.

 reget /path/to/file/on/server C:pathtolocalfile reput C:pathtolocalfile /path/to/file/on/server 

To continue moving the directory, just add the -r parameter :

 reput -r Desktop/bin bin 

If the path to the file contains spaces, put it in quotation marks.

This command does not work:

 put C:Program Files (x86)SteamsteamappscommonThe Witcher 3SOUNDTRACKThe_Witcher_3_Wild_Hunt_-_Official_Soundtrack_(steam_edition)_mp3.zip 

But the following command has:

 put "C:Program Files (x86)SteamsteamappscommonThe Witcher 3SOUNDTRACKThe_Witcher_3_Wild_Hunt_-_Official_Soundtrack_(steam_edition)_mp3.zip" 

This article covers the most important things to know about SFTP. Hope you are succesful.

5 ★ | 1 Vote