Linux Sftp Command With Examples – POFTUT

Linux Sftp Command With Examples

File Transfer Protocol is a very popular protocol for transferring files between hosts. FTP is a fast and practical protocol not secure. In old times security was no problem but into days world security is important. So new protocols and ways developed to make FTP like operations more secure. Secure FTP or SFTP is a protocol developed for secure file transfers.  SFTP works over SSH protocol and provides abilities and features of FTP. SSH by default runs on port 22/TCP. In this tutorial, we will look at usage and examples of SFTP. Windows alternative for sftp client is Putty SFTP or Psftp which is provided by putty tools. Sftp is a command-line tool where you have to work with bash shell or MS-DOS but if you need a GUI for SFTP you can use the FileZilla which also supports SFTP protocol. But if you need to download and upload a file between two different remote servers which do not have GUI or ability to install applications the SFTP is the best tool.

sftp Command Syntax

sftp command has the following command. The user is the username of the remote host. The host is the remote host domain name or IP address.

sftp [user@]host[:file ...]


sftp [user@]host[:dir[/]]


sftp -b batchfile [user@]host

Connect To Sftp

The first step to used sftp is connecting a server. As stated before sftp uses ssh protocol and a connection will be made to the ssh server. Authentication is done according to the servers ssh connection configuration which generally a username, password authentication.

$ sftp ubu1

Or different user name can be explicitly defined

$ sftp root@ubu1
Connect To Sftp
Connect To Sftp

Man and Help Sftp

To get more and detailed help there is two way exists. First one is a simple and fast way with a help option.

$ sftp -h
Man and Help Sftp
Man and Help Sftp
$ man sftp
Man and Help Sftp
Man and Help Sftp

sftp Command Help with ?

After connecting an sftp server the commands provided by this server can be listed like below.

Command Help
Command Help

As we see there is a command like cd, chgrp, chmod, exit, ls etc. We will look at these commands in detail next examples.

LEARN MORE  What Is HTTP (Hypertext Transfer Protocol)?

Current Working Directory On Remote with pwd Command

The current working directory is the path currently active all issued commands works according to the current working directory. For example, to download the centos.iso given command will look current working directory. This is how can we get the current working directory.


Current Working Directory On Local with lpwd Command

There is a local current working directory for sftp. When downloading files by default files will be downloaded to the local current working directory.


List Files On Remote with ls Command

To download files we should know the exact file names. The best way to get file names exactly is by listing them with the list command.

List Files On Remote
List Files On Remote

List Files On Local system with lls Command

You can list local system file from the sftp interactive command line. Use the lls command where the first l means the local .


Upload File To Remote with mput Command

Local files can be uploaded to the remote SFTP server by using the mput command. The first letter of them mput command which is shortform of the multiple means put or upload multiple files. In the following example we will upload the file named tmux.tar.gz file into the remote SFTP server.

mput tmux.tar.gz

Upload Multiple Files To Remote with mput Command

Multiple files can be uploaded like below. To upload by using globbing file extension, generic names can be used.

mput *
Upload Multiple Files
Upload Multiple Files


mput *.bak

Upload Directory To Remote with put Command

As we see the previous example mput command is only used for files. To upload directories there is another command to use.

put -r *
Upload Directory To Remote
Upload Directory To Remote

This will put all files and directories to the remote working directory.

Download File To Local with get Command

Another popular usage of sftp is downloading files from remote with get .

get tmux.tar

Download Directories To Local with get Command

Downloading directories recursively done with get command. To get directories and subdirectories recursively -r parameter is provided like below.

get -r test3
Download Directories
Download Directories

Switch Directory On Remote with cd Command

The current working directory for remote can be changed with the simple command cd .

cd tmux
Switch Directory On Remote
Switch Directory On Remote

Switch Directory On Local with lcd Command

Current working directory on local can be changed similar to the remote.

lcd ..
Switch Directory On Local
Switch Directory On Local

Create Directory On Remote with mkdir Command

Directories can be created on remote sftp server the same as Linux operating system command mkdir .

mkdir test
Create Directory On Remote
Create Directory On Remote

Create Directory On Local with lmkdir Command

Same as remote but prefixed with l

lmkdir test

Remove File On Remote with rm Command

The file can be removed from remote sftp server with rm command

rm tmux.tar

Remove Directory On Remote with rmdir Command

Removing directory command is different from file remove command on the remote .

rmdir test

Exit From Interactive SFTP Shell

We can exit from the interactive SFTP shell by using different commands like ! or exit . Both the ! and exit will close the connection to the remote SFTP server and exit from the current SFTP interactive shell.




1 thought on “Linux Sftp Command With Examples”

Leave a Comment