File Transfer Protocol (FTP) was a widely used protocol for remotely transferring files or data in an unencrypted format, which is not a secure method of communication. As we all know, File Transfer Protocol is not at all secure because all transmissions occur in clear text, and the data can be read by anyone sniffing the network packets.
So, in general, FTP should only be used in specific situations or on networks that you trust. SCP and SSH have addressed this security ambiguity over time, adding a secure encrypted layer while transferring data between remote computers.
To establish a secure connection, SFTP (Secure File Transfer Protocol) runs over SSH protocol on standard port 22 by default. SFTP has been integrated into a number of graphical user interface (GUI) tools (FileZilla, WinSCP, FireFTP etc.).
Security Caution: Please do not open the SSH port (Secure SHell) globally as this would be a security breach. You can only open for specific IP addresses from which you intend to transfer or manage files on the remote system, or vice versa.
This article will walk you through ten sftp command examples to use with an interactive command-line interface.
1. How to Connect to SFTP
The same SSH protocol is used by default to authenticate and establish an SFTP connection. At the command prompt, enter the username and remote hostname or IP address to begin an SFTP session. After successful authentication, you will see a shell with an sftp> prompt.
[root@tecmint ~]# sftp email@example.com Connecting to 22.214.171.124... firstname.lastname@example.org's password: sftp>
2. Getting Help
When you’re in the sftp prompt, check the available commands by typing ‘?’ or ‘help’ into the command prompt.
sftp> ? Available commands: cd path Change remote directory to 'path' lcd path Change local directory to 'path' chgrp grp path Change group of file 'path' to 'grp' chmod mode path Change permissions of file 'path' to 'mode' chown own path Change owner of file 'path' to 'own' help Display this help text get remote-path [local-path] Download file lls [ls-options [path]] Display local directory listing ln oldpath newpath Symlink remote file lmkdir path Create local directory lpwd Print local working directory ls [path] Display remote directory listing lumask umask Set local umask to 'umask' mkdir path Create remote directory put local-path [remote-path] Upload file pwd Display remote working directory exit Quit sftp quit Quit sftp rename oldpath newpath Rename remote file rmdir path Remove remote directory rm path Delete remote file symlink oldpath newpath Symlink remote file version Show SFTP version !command Execute 'command' in local shell ! Escape to local shell ? Synonym for help
3. Check Present Working Directory
The command ‘lpwd‘ is used to check the current working directory on the local machine, whereas the command ‘pwd‘ is used to check the remote working directory.
sftp> lpwd Local working directory: / sftp> pwd Remote working directory: /tecmint/
4. Listing Files
Listing files and directories on the local and remote systems.
- On Remote:
- On Local:
5. Upload File
Insert one or more files into the remote system.
sftp> put local.profile Uploading local.profile to /tecmint/local.profile
6. Upload Multiple Files
Adding several files to the remote system.
sftp> mput *.xls
6.1. Download Files
Obtaining single or multiple files from the local system
sftp> get SettlementReport_1-10th.xls Fetching /tecmint/SettlementReport_1-10th.xls to SettlementReport_1-10th.xls
Get access to multiple files on a local system.
sftp> mget *.xls
Note: As we can see by default, getting a command downloads a file with the same name in the local system. We can download a remote file with a different name by including it at the end of the URL. (This only applies when downloading a single file.)
7. Switching Directories
Transferring files from one directory to another in both local and remote locations.
- On Remote:
sftp> cd test sftp>
- On Local:
sftp> lcd Documents
8. Create Directories
Making new directories in both local and remote areas.
sftp> mkdir test sftp> lmkdir Documents
9. Remove Directories
Remove the remote system’s directory or file.
sftp> rm Report.xls sftp> rmdir sub1
Please keep in mind that in order to remove or delete a directory from a remote location, the directory must be empty.
10. Exit sFTP Shell
The ‘!’ command takes us to a local shell where we can run Linux commands. Where we can see sftp> prompt return, type the command ‘exit‘.
sftp> ! [root@sftp ~]# exit Shell exited with status 1 sftp>
The SFTP protocol is a very useful tool for managing servers and transferring files to and from them (Local and Remote). We hope that these tutorials have helped you understand how to use SFTP to some extent.