top sftp command examples

Top SFTP Command Examples

SFTP is a secure file transfer protocol used popularly in Linux to transfer files. It is much better than normal FTP as it encrypts data during transmission and prevents it from being deciphered by network sniffers. SFTP runs over SSH protocol to use a secure connection, and is also a part of popular FTP services such as FileZilla, WinSCP, FireFTP, etc. In this article, we will look at the top SFTP command examples to work with SFTP. You can keep this list handy for regular reference.


Top SFTP Command Examples

Here are top 10 SFTP command examples for your reference.


1. Connect to SFTP

Here is the syntax to connect to your SFTP server.

$ sftp username@ip_address

Here is an example to connect to SFTP. Once you enter the command to connect to SFTP server, you will be prompted for password. On entering the correct password, you will be logged into SFTP with sftp> prompt as shown below.

$ sftp ubuntu@54.43.32.21

Connecting to 54.43.32.21...
ubuntu@54.43.32.21's password:
sftp>


2. Get Help

Once you have logged into SFTP, you can easily get help information such as available commands using ? key.

sftp> ?
Available commands:
cd path                       Change remote directory to 'path'
lcd path                      Change local directory to 'path'
...


3. Find out present working directory

If you want to find out your present working directory there are two available commands – lpwd for local working directory and pwd for remote working directory.

Here is the command to find out local present working directory.

sftp> lpwd

Here is the command to find out remote present working directory.

sftp> pwd


4. Listing Files

Here too, there are two commands to list files – ls to list files in remote machine and lls to list files on local machine.

Here is an example to list files on remote machine.

sftp> ls

Here is the command to list files on local machine.

sftp> lls


5. Upload File

You can upload file to SFTP server using put command. Here is an example to upload file. You need to mention file name or path after put command. If you only mention file name and not full file path then sftp will look for the file in your present working directory.

$ sftp> put data.txt
Uploading data.txt to /ubuntu/data.txt


6. Upload Multiple Files

You may also use wildcard characters like asterisk (*) to upload multiple files. But if you are uploading multiple files, use mput command.

sftp> mput *.txt


7. Download File

You can download single file using get command.

sftp> get data.txt
Fetching /ubuntu/data.txt to data.txt

If you want to change the name of downloaded file, just specify its new name at the end

sftp> get data.txt new_data.txt


8. Download Multiple Files

Similarly, you can download multiple files using mget.

sftp> mget *.txt


9. Change directory

You can change local directory using lcd command and remote directory using cd command.

Here is the command to change remote directory.

sftp> cd test

Here is the command to change local directory.

sftp> lcd data


10. Create Directory

You can create local directory using lmkdir and create remote directory using mkdir commands. Here is the command to create remote directory.

sftp> mkdir test

Here is the command to create local directory.

sftp> lmkdir test


11. Remove remote files & directory

You can rm command to remove remote files and rmdir command to remove remote directories. Please note, when you use rmdir command, the remote directory must be empty for deletion to happen.

$ rm data.txt
$ rmdir files


12. Exit SFTP Shell

If you want to temporarily exit SFTP shell to run Linux commands just enter bang (!) symbol. You will be sent to a Linux shell where you can run Linux commands. Once you are done, enter exit command to return back to SFTP shell.

sftp> !
ubuntu$ date
Mon Aug 23 03:33:26 UTC 2021

ubuntu$ exit
sftp>

If you want to permanently exit SFTP shell enter exit, bye or quit command.

In this article, we have learnt many different commands to work with SFTP. Basically, most of these commands are similar to the ones used in Linux shell/terminal. E.g cd command changes directory. For most of these commands there are two counterparts – one for local machine and the other for remote machine. The remote command is similar to the Linux command (e.g cd changes remote directory), and its local counterpart can be obtained by prefixing ‘l’ (letter el) to the remote command (e.g lcd changes local directory).

Also read:

How to Use Journalctl command in Linux
How to Grep Log File Within Specific Time Period
Top Yum command examples in Linux
How to Password Protect Folders in Linux
How to Restrict SSH Users to Specific Folder

Leave a Reply

Your email address will not be published. Required fields are marked *