FTP traffic is unencrypted and insecure which is why it has been mostly replaced by SFTP. SFTP runs over SSH protocol by default on TCP port 22 and offers the same set of security and encryption capabilities as SSH. Default SSH daemon running as part of OpenSSH server on Linux systems supports basic features of SFTP protocol by default though there is separate dedicated software available like vsftpd which can be configured to get extra features and customizations. In this article, we’ll cover the usage of SFTP from the command line. I’ll be using an Ubuntu system though commands listed here will work on any Linux system with sftp client. Before getting into commands, you should know that SCP is getting deprecated and as an alternative, it’s good to get familiar with the SFTP command. You can do pretty much everything with SFTP that you do with SCP.

Copying files

SFTP can be used as a replacement for SCP (Secure Copy) command on some supported use cases. One such case is using SCP to push or pull files from a remote server in one go. The syntax for uploading using the SCP command goes like this: And for downloading like this: Similarly, we can use the following sftp command syntax to upload files to a remote server: Below is one demo showing uploading of files using sftp as a one-liner: To download a file from a remote server, use the below command syntax: Here’s a demo of downloading a file in one line using sftp: You may also exchange the SSH key for password-less authentication.

Connecting to an SFTP server

To initiate an SFTP connection, use sftp command with a username and remote host’s name or IP. Default TCP port 22 should be open for this to work or else explicitly specify the port using -oPort flag. I’m connecting to an SFTP server with IP 192.168.1.231. The first time you connect to an SFTP server, you’ll be prompted to confirm the server fingerprint like SSH. Once confirmed by typing ‘yes‘ connection proceeds and prompts for a user’s password. On a successful connection, you’ll be shown sftp> prompt.

Check Version

You can check the SFTP version using version command at sftp prompt.

Getting Help

To get help about available commands and syntax for SFTP, use ‘?‘ or ‘help‘.

Show Working Directory

When connected to a remote server, you can show the present working directory of the remote system using pwd command. To show the local system’s present working directory use lpwd command.

List Files

You can list files in the remote working directory using ls command. To list files in the local working directory, use lls command.

Switching Directories

Switching the remote working directory can be done using cd command. Refer example below: To switch local working directory, use lcd command. Below is a simple example to show usage of lcd.

Upload Files

To upload a single file, use put command. See how I upload local file1 to remote working directory using put command. I can verify it using ls command which prints the content of remote working directory. To upload multiple files in one go, we can use mput command as shown below. I use mput with a regular expression pattern file[23] which basically uploads file2 and file3 and skips file1 as it has already been uploaded in the previous step. You can use any wildcard or regular expression with mput.

Download Files

Single file using SFTP can be downloaded using get command. Here’s an example where I downloaded remote_file4 using sftp: To download multiples files, use mget command. I’m downloading here all files that matches pattern remote_file* in remote working directory to my local working directory. I finally use lls command to see the downloaded files.

Create Directory

A new directory can be created on a remote server using mkdir command. Similarly, if you want to create a new directory on local system’s current working directory from sftp prompt, use lmkdir command.

Remove Directory

An empty remote directory can be removed using rmdir command. Do note that if it is not empty, you’ll get an error.

Remove File

A remote file can be removed using rm command.

Rename File

A remote file can also be easily renamed using rename command.

Filesystem Usage

To display statistics for the current directory or filesystem containing ‘path’, use df command. We can use -h flag to show statistics in a human-readable format. Do note that the statistics shown are for the remote SFTP server’s respective filesystem and not the local machine’s filesystem.

Quit SFTP Session

To quit the SFTP session, use either bye, exit, or quit command. You’ll be returned to your OS prompt after exiting SFTP.

Conclusion

SFTP is one of the best options available which is secure as well as easy to use. It offers CLI as well as GUI features and is supported across different platforms. Refer sftp man page for further reading. If interested in learning more then check out this Udemy Linux Mastery course.

15 Examples of SFTP command in Linux - 1315 Examples of SFTP command in Linux - 8315 Examples of SFTP command in Linux - 3015 Examples of SFTP command in Linux - 4915 Examples of SFTP command in Linux - 7815 Examples of SFTP command in Linux - 42