SFTP is a protocol that offers a secure and private channel for transfer of files between systems using encryption. A misconception exists about the acronym SFTP, with some thinking it stands for Secure File Transfer Protocol. It stands for SSH File Transfer Protocol. Where in ‘FTPS’ is different, FTPS client will first check if the ftps server’s certificate is trusted and use Secure Sockets Layer protocol (TLS).
One may be forgiven to think that SFTP and FTP are similar in terms of functionality but, the two employ varied protocols. You, therefore, cannot use a standard FTP client to connect to an SFTP server. In this guide, we will focus on the commonly used SFTP commands.
Read Also: 12 lftp Commands to Manage Files with Examples
The standard application of SFTP is to run on a command interface within its own environment. That is why in most cases you will notice the program interface changing to the prompt sftp>. The moment you have invoked the SFTP session, the usual system commands will not execute unless you call them using a specific language that is in the SFTP command line standard.
Not all computers can process SFTP commands. You can choose to use the graphical interface version of SFTP or the command line depending on the Operating System you are using. The GUI interface requires you to install an SFTP utility.
In this article, we will take you through some SFTP commands examples that you can use via the unix/linux command line.
How to Connect With SFTP
The SSH protocol used to establish communication are the same as those required by an SFTP channel. Most people prefer to use saved passwords set as defaults, but I would recommend the use of SSH keys that you can use when you need to access any system.
To start an SFTP session, you need a username and the remote hostname. Alternatively, you can use the IP address of the host name at the prompt like shown below:
~ # sftp [email protected]
[email protected]‘s password:
Connected to [email protected]
In the above example, if there were a connection that allows the processing of the above command, you would expect a password prompt before gaining access.
1) How to Get Help at the Prompt
If you have no clue on the type or format that can be used on an SFTP command line, use the “?” or “help” at the prompt as follows
sftp ?
2) Confirm the Working Directory
Using the command lpwd will give more information on the working directory. The pwd is used to check the remote working directory.
sftp> lpwd
Output
Local working directory: /root
sftp> pwd
Output
Remote working directory: /upload
3) Listing Files
At the SFTP command prompt, you list both remote and local files using different commands.
Remote listing
sftp> ls
Local listing
sftp> lls
4) Uploading Files
Uploading can take place by placing single or multiple files on the remote host.
Single file on the remote host use,
sftp> put Hello-World.txt
Output
Uploading Hello-World.txt to /upload/Hello-World.txt
Hello-World.txt
Multiple files on the remote host
sftp> mput *.txt
Output
Uploading Hello-World.txt to /upload/Hello-World.txt
Hello-World.txt 100% 0 0.0KB/s 00:00
Uploading file1.txt to /upload/file1.txt
file1.txt 100% 0 0.0KB/s 00:00
Uploading file2.txt to /upload/file2.txt
file2.txt 100% 0 0.0KB/s 00:00
Uploading file3.txt to /upload/file3.txt
file3.txt 100% 0 0.0KB/s 00:00
Uploading file4.txt to /upload/file4.txt
file4.txt 100% 0 0.0KB/s 00:00
5) Downloading Files
You will be able to download single or multiple files in a local-path or system.
sftp> get file1.pdf
Output
Fetching /upload/file1.pdf to file1.pdf
Download multiple files on a local-path or system
sftp> mget * .pdf
Output
Fetching /upload/file1.pdf to file1.pdf
Fetching /upload/file2.pdf to file2.pdf
Fetching /upload/file3.pdf to file3.pdf
Fetching /upload/file4.pdf to file4.pdf
Fetching /upload/file5.pdf to file5.pdf
It is evident that when downloading a file in the local system is done using the same name. When you want to use a different name on a remote file download, the name should be specified at the end of the command.
6) Switching Directories
On the remote server, you use the command,
sftp> cd test
On the local machine, you use the command,
sftp> lcd Documents
7) Creating directories
Creating directories on both remote and local paths is possible
A new directory on the local path
sftp> mkdir test
A new directory on a remote host
sftp> lmkdir Documents
8) Removing Directories
Removing a directory or file in remote hosts
Removing a file in remote hosts
sftp> rm Report.xls
Removing directory in remote hosts
sftp> rmdir Department
Note: This command will only work if the target directory is empty
9) Exiting the Command Shell
The exclamation mark! (known as a command in this case) is used to get out of the SFTP command prompt as shown in the following example.
sftp>!
[[email protected] ~]# exit
Shell exited with status 1
sftp>
As simple as it may look like, SFTP is a very powerful tool used for administering servers and managing file transfers between hosts. The utility can be used on both remote and local servers.