7 SFTP Command Line Examples In Linux

SFTP Command Line Examples
SFTP command examples

Today we will present you with several useful SFTP command-line examples. We are all familiar with the FTP (File Transfer Protocol) and if you are reading this article you’ve probably used it for transferring files to your virtual server and your local computer.

FTP is a very simple protocol, but it is also very insecure. All transmissions, including the username and password, are transferred in clear text and it does not use any type of security or encryption while transferring the data. It means that all data can be read by anyone who may intercept the transmission. Luckily, there is a great and secure replacement for the FTP protocol – SFTP. In this tutorial, we will focus on 7 Useful SFTP command examples.

linux sftp command

SFTP stands for Secure File Transfer Protocol and it relies on SSH. SFTP allows users to transfer data over a connection that is secured using the well-known Secure Shell (SSH) protocol. The SFTP protocol is also faster and uses less data than the FTP protocol. Therefore we highly recommend using SFTP commands, instead of FTP whenever possible. With SFTP commands, you can transfer your files between your machines using some of the popular SFTP clients such as FileZilla, Cyberduck, or WinSCP. You can also do it directly from the Linux command line.

In this article, we will demonstrate and explain some of the most popular and useful SFTP commands.

 

1. Connect to the SFTP server

You can connect to a remote server via SFTP using the following command

sftp  user@IP_Address

user@IP_Address' password:
Connected to IP_Address.

You can also use a hostname instead of the IP address.

We already mentioned that SFTP is using the same protocol as SSH, so if SSH is not listening on the default port 22, you can specify the port in the command

sftp -oPort=2345 user@IP_Address

Where ‘2345’ is the port number you are using.

Linux SFTP command2. List SFTP files in the current directory

The SFTP command to list all files is ls , so that you can list all files and directories in the current working directory, as shown below.

 

sftp> ls
directory directory1  file  file.txt file.zip

You can also list the files on the local system

sftp> lls

3. Navigate through directories

To find the current working directory on the local server use

sftp> pwd
Remote working directory: /home/user

for the local server, use the following command

sftp> lpwd
Local working directory: /

You can easily change the working directory in SFTP. For the remote server use

sftp? cd directory

and for the local server use

sftp> lcd directory

4. Upload files and directories using the put command

You can upload single or multiple files or directories from the local machine to the remote one.

sftp> put filename

To upload multiple files use the following command

sftp> mput file1 file2 file3

To upload a directory to the remote server, you have to create the destination directory on the remote server first, and then start the upload

sftp> mkdir directory
sftp> put -r directory/

5. Download files and directories using the get command

Download a single file from the remote to the local machine

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
sftp> get file

or download multiple files with the ‘mget’ command

sftp> mget file1 file2 file3

Download a directory and all its content with the following command

sftp> get -r directory

6. Create and remove directories

Create a new directory on the remote server

sftp> mkdir newdirectory

Create a new directory on the local server

sftp> lmkdir newdirectory

7. Getting Help

You can find all SFTP commands with a short description by executing ‘help’ or ‘?’.

sftp> ?

Available commands:
bye                                Quit sftp
cd path                            Change remote 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'
df [-hi] [path]                    Display statistics for current directory or
filesystem containing 'path'
exit                               Quit sftp
get [-Ppr] remote [local]          Download file
reget remote [local]            Resume download file
help                               Display this help text
lcd path                           Change local directory to 'path'
lls [ls-options [path]]            Display local directory listing
lmkdir path                        Create local directory
ln [-s] oldpath newpath            Link remote file (-s for symlink)
lpwd                               Print local working directory
ls [-1afhlnrSt] [path]             Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-Ppr] local [remote]          Upload file
pwd                                Display remote working directory
quit                               Quit sftp
rename oldpath newpath             Rename remote file
rm path                            Delete remote file
rmdir path                         Remove remote directory
symlink oldpath newpath            Symlink remote file
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help

sftp command line examples

Of course, you don’t have to use sftp command in Linux with those examples if you use our Linux VPS Hosting services. Free Full Managed Support is included in all our VPS Hosting packages so you can simply ask our expert Linux admins to use SFTP commands for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on Linux SFTP Commands,  please share it with your friends on social networks or simply leave a reply. Thanks.

10 thoughts on “7 SFTP Command Line Examples In Linux”

    • Yes, you can by changing the PS1 value in .bashrc file, for example:

      PS1=’${debian_chroot:+($debian_chroot)}\[\033[01;35m\]\u@\h\[\033[00m\]:\[\033[01;34m\] \w\[\033[01;37m\] > ‘

      Reply
  1. Yes, this is great for an “ssh” connection, what about an “sftp” connection? The prompt is without color and always “sftp>” no matter what server you are connecting to. Thanks.

    Reply
  2. How to transfer 2 different files in a single command using sftp.
    Actually I tried with mput command . But it is taking the first argument after mput as source file and second argument as destination file.
    Example:
    sftp> mput
    It is trying to copy file1 to file2 . This is happening only in sftp and it is working fine when using ftp.
    can someone please help me to solve using sftp.

    Reply
    • Using MPUT you can upload multiple files from a local directory to a remote directory. Are you trying to upload or download the files? To download multiple files from a remote directory to a local directory you can use MGET.

      Reply
  3. Yes , I am trying to upload the file from local to remote using mput as below.
    sftp> mput file1 file2
    But it is taking file1 as source file and file2 as destination file path.
    But i need to upload file1 and file2 to remote server in a single command.

    Reply
  4. If I am using SFTP to push an empty file from Linux to the Mainframe to create a primer file in generation 1 of the dataset, how do I specify the file specifications so that version one has the same file specs that were defined when the dataset was created?

    Reply
  5. I want to create a directory in sftp via script, but first i need to check whether the directory already exists in sftp. Can anyone help on this. Btw i am not having ssh access to sftp server.

    Reply
    • You can use any FTP client (such as FileZilla) to access your server and list the current directories to see if the directory in question already exists, or use the command to list (ls) as per the tutorial on this page.

      Reply

Leave a Comment