Open menu

Learn

Windows SFTP Command Line: A Comprehensive Guide for Secure File Transfers

Setting up secure file transfers using the Windows SFTP command line? You’ve come to the right place. Learn to swiftly establish SFTP on your Windows system, execute file transfers, and manage your connections with ease. Expect to dive into essential commands and configurations without the fluff, getting right to the meat of SFTP on Windows.

Setting Up SFTP on Windows

Beginning with version 1803 (build 17134) of Windows 10, Microsoft has introduced OpenSSH as an optional feature accessible through the Command Prompt application. This addition enables users to utilize SFTP (Secure File Transfer Protocol) directly via the command line, facilitating secure file transfers between systems. For guidance on activating this feature, please refer to this detailed article that provides step-by-step instructions on how to enable OpenSSH in Windows 10 and Windows 11. Windows SFTP command line setup

Establishing an SFTP Connection on Windows via the Command Line

An SFTP connection, also known as SSH File Transfer Protocol, allows you to perform file transfers via a secure connection. An SFTP session is initiated by using the command sftp user@host , which allows the use of file transfer commands thereafter. The login format required is similar to the one used by SSH, using the user’s username and either a password or SSH keys for authentication. Let's see how to establish a connection. 1. Open Command Prompt : Press Windows + R , type cmd , and press Enter to open the Command Prompt. 2. Initiate SFTP Connection :
sftp [username]@[host]
Replace [username] with your username on the SFTP server and [host] with the server's address or IP. For example:
sftp user@us-east-1.sftpcloud.io
3. Enter Your Password : You'll be prompted to enter your password for the SFTP server. Input your password when prompted (note that it won't be visible).

Navigating Directories in SFTP

Effective directory navigation is key in SFTP. Whether you’re on your local system or the remote server, SFTP provides commands akin to traditional shell commands for file and directory manipulation. You can navigate to local directories using the ‘lcd’ command and remote directories using the ‘cd’ command. It’s also possible to switch to the local shell from within an SFTP session by typing an exclamation mark ‘!’. Next, we will examine in depth the navigation of remote working directory as well as local directories.

Remote Directory Navigation

Navigating remote directories effectively can enhance your SFTP experience. The pwd command prints the current remote directory path, showing you where you’re located on the remote file system.
pwd
The cd command changes the current directory, such as ‘cd Documents’ to switch to the Documents directory.
cd Documents
The ls command displays the contents of the current remote directory, with the -l flag providing a detailed list, and the -a flag showing hidden files and directories.
ls 

# show hidden files and directories 
ls -a 

# provide detailed list 
ls -l
These commands facilitate efficient navigation of remote directories. Now, let’s turn our focus to local directories.

Local Directory Navigation

Navigating local directories during an SFTP session is just as crucial as navigating remote directories. The lpwd command (local present working directory) displays the current working directory on the local system.
lpwd
The lcd command changes the current working directory on the local file system. This command affects where files are uploaded from or downloaded to.
lcd
The lls command is used to list files and directories in the local working directory. It provides a convenient way to view the contents of the current folder.
lls
With these commands in your arsenal, navigating local directories becomes a breeze. Having mastered directory navigation, we can now focus on transferring files using SFTP.

Transferring Files Using SFTP

Transferring files securely is the heart of SFTP, a file transfer protocol that utilizes encryption for both commands and data. Files are uploaded to the remote directory using the ‘put’ command, provided the user has write permissions on the server. Similarly, the ‘get’ command copies files from the remote directory to the user’s local directory. Transferring a directory is slightly different as it requires the directory to be compressed locally and then transferred as a file using the ‘put’ command. With this knowledge, let’s explore how to transfer files by uploading and downloading them.

Uploading Files to the Remote Server

Uploading files to a remote server via SFTP (Secure File Transfer Protocol) from the Windows command line involves a straightforward process. To upload a single file, use the put command followed by the path of the file on your local machine and the target path on the remote server (optional):
put local_file_path remote_file_path
If you want to upload the file to the current directory on the server, you can omit the remote_file_path , and the file will be uploaded to the current directory you navigated to (or the home directory if you didn’t change directories). Now that we’ve mastered uploading, let’s learn how to download files from a remote server.

Downloading Files from Remote Server

One of the various SFTP commands is the ‘get’ command which allows for the transfer of files from a remote server to the local system, simplifying the process of accessing and managing files.
get remote_filename /local/path/for/file
Having covered file transfers, we can now proceed to manage files and directories in SFTP.

Managing Files and Directories in SFTP

SFTP file management Effective management of files and directories is a valuable skill in SFTP. Familiarity with commands such as ‘mkdir’ for creating a new directory, ‘rmdir’ for removing an empty directory, and ‘rename’ for changing file or directory names can significantly enhance your SFTP experience. These commands, prefixed with ‘l’, pertain to the local system, like ‘lpwd’, ‘lcd’, ‘lrmdir’, whereas commands without ‘l’ affect the remote system, such as ‘pwd’, ‘cd’, ‘rmdir’. Next, we will explore more about creating and deleting directories, as well as renaming and deleting files.

Creating and Deleting Directories

Creating and deleting directories is a fundamental operation in SFTP. A directory on the remote server can be created using the mkdir command, and the rmdir command is used to remove a directory from the remote server. These commands allow you to manage your file structure effectively. However, as you create and delete directories, there might also be a need to rename or delete files.

Renaming and Deleting Files

Renaming and deleting files are key operations in managing your files in SFTP. The ‘rename’ command is used to modify the name of files on the remote server. This helps in managing file organization and maintaining clarity within the server’s directories. To rename a file, the command format is ‘rename oldfilename newfilename’, replacing ‘oldfilename’ with the current file name and ‘newfilename’ with the desired new name. The ‘lrename’ command allows you to rename files on your local machine while connected to an SFTP session. The ‘rm’ command can be used to delete files from the remote server. It is important to use this command carefully to avoid accidental deletion of important files. To remove a file, type ‘rm filename’, where ‘filename’ is the name of the file to be deleted. The ‘lrm’ command is used to delete files from the local system during an SFTP session. With these commands, you can effectively manage your files and directories.

Summary

Mastering SFTP via the command line is a valuable skill in today’s interconnected digital world. From setting up SFTP on Windows, through transferring files, to troubleshooting common issues, you now possess the knowledge to navigate the realm of secure file transfers effectively. Remember, every journey begins with a single step. So, why not take that step in the world of SFTP today?