10 sFTP Command Examples to Transfer Files on Remote Servers in Linux

File Transfer Protocol (FTP) was widely used protocol to transfer files or data remotely in unencrypted format which is not secure way to communicate. As we all know that File Transfer Protocol is not at all secure because all transmissions happens in clear text and the data can be readable by anyone during sniffing the packets on the network.

sftp commands

10 sftp command examples

So, basically FTP can be used in limited cases or on the networks that you trust. Over the period of time SCP and SSH addresses this security ambiguity and added an encrypted secure layer while transferring data between remote computers.

SFTP (Secure File Transfer Protocol) runs over SSH protocol on standard port 22 by default to establish a secure connection. SFTP has been integrated into many GUI tools (FileZilla, WinSCP, FireFTP etc.).

Security Warnings: Please don’t open SSH port (Secure SHell) globally as this would be a security breaches. You can only open for specific IP from where you are going to transfer or manage files on remote system or vice versa.

  1. 5 Best Practices to Secure and Protect SSH Server
  2. 10 Wget Command Examples in Linux

This article will guide you 10 sftp command examples to use it through interactive command-line interface.

1. How to Connect to SFTP

By default, same SSH protocol is used to authenticate and establish a SFTP connection. To start an SFTP session, enter the username and remote hostname or IP address at the command prompt. Once authentication successful, you will see a shell with an sftp> prompt.

[[email protected] ~]# sftp [email protected]

Connecting to 27.48.137.6...
[email protected]'s password:
sftp>

2. Getting Help

Once, you in the sftp prompt, check the available commands by typing ‘?‘ or ‘help‘ at 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 Local present working directory, whereas ‘pwd‘ command is used to check Remote working directory.

sftp> lpwd
Local working directory: /
sftp> pwd
Remote working directory: /tecmint/
  1. lpwd – print the current directory on your system
  2. pwd – print the current directory on the ftp server

4. Listing Files

Listing files and directories in local as well as remote system.

On Remote
sftp> ls
On Local
sftp> lls

5. Upload File

Put single or multiple files in remote system.

sftp> put local.profile
Uploading local.profile to /tecmint/local.profile

6. Upload Mutiple Files

Putting multiple files on in remote system.

sftp> mput *.xls

6. Download Files

Getting single or multiple files in local system.

sftp> get SettlementReport_1-10th.xls
Fetching /tecmint/SettlementReport_1-10th.xls to SettlementReport_1-10th.xls

Get multiple files on a local system.

sftp> mget *.xls

Note: As we can see by default with get command download file in local system with same name. We can download remote file with different name specifying the name at the end. (This applies only while downloading single file).

7. Switching Directories

Switching from one directory to another directory in local and remote locations.

On Remote
sftp> cd test
sftp>
On Local
sftp> lcd Documents

8. Create Directories

Creating new directories on local and remote locations.

sftp> mkdir test
sftp> lmkdir Documents

9. Remove Directories

Remove directory or file in remote system.

sftp> rm Report.xls
sftp> rmdir sub1

Note: To remove/delete any directory from remote location, the directory must be empty.

10. Exit sFTP Shell

The ‘!‘ command drop us in local shell from where we can execute Linux commands. Type ‘exit‘ command where we can see sftp> prompt return.

sftp> !

[[email protected] ~]# exit
Shell exited with status 1
sftp>

Conclusion

The SFTP is a very useful tool for administrating servers and transferring files to and from (Local and Remote). We hope this tuts will help you to understand the usage of SFTP in some extent.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Narad Shrestha

He has over 10 years of rich IT experience which includes various Linux Distros, FOSS and Networking. Narad always believes sharing IT knowledge with others and adopts new technology with ease.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

51 Responses

  1. Deepak Shiv says:

    Hello Experts,

    Any idea how to get latest file on sftp. I have tried with multiple options but it isn’t working:

    # ls -1tr file_name | tail -1
    

    In local UNIX box above command works perfectly but same when I tried to execute on SFTP it gives complete list and not latest file.

    Is there any specific command that can be used for getting only one latest file out of multiple files

  2. HocAutomation says:

    How to resolve this error when connect to sftp?

    Error: Disconnected: No supported authentication methods available (server sent: publickey,gssapi-keyex,gssapi-with-mic)
    Error: Could not connect to server

  3. Chitvan says:

    @Ravi Saive,

    I have got the solution for my issue. Actually the password I was trying to copy past in UNIX box had dollar sign and Euro sign (£$). Whenever I pasted the password these converted into another character (^A). So the password was not identified and failed every time.

    After resetting the password to simple values I am able to login and also transfer the files.

    Thanks again for your Support Ravi.

  4. Chitvan says:

    I am unable to login to a server. I have the username and password and firewall also open but its saying wrong password.

    AZA1UATXXAML004:ef>/ > sftp -oPort=10023 [email protected]
    Connecting to 22.113.232.22...
    SSH Server supporting SFTP and SCP
    password
    Enter password for DTUIT98101UAT
    Password:
    password
    Enter password for DTUIT98101UAT
    Password:
    password
    Enter password for DTUIT98101UAT
    Password:
    Received disconnect from 22.113.232.22: 11: Too many bad authentication attempts!
    Couldn't read packet: Connection reset by peer
    
    • Ravi Saive says:

      @Chitvan,

      Are you sure you have opened port 10023 on Firewall? Also “DTUIT98101UAT” is your username?

      • Chitvan says:

        Yes, while doing telnet I am able to connect to the server 22.113.232.22.

        See below telnet output:

        AZA1UATXXAML004:ef>/ > telnet 22.113.232.22 10023
        Trying 22.113.232.22...
        Connected to 22.113.232.22.
        Escape character is '^]'.
        SSH-2.0-SFTP Server
        

        But when I want to sftp, then its not accepting the password. Yes DTUIT98101UAT is the username.

        However, when I am running below command it seems to be connected.

        lftp sftp://DTUIT98101UAT:[email protected]:10023
        

        See below:

        AZA1UATXXAML004:ef>/ > lftp sftp://password:@22.113.216.20:10023
        lftp [email protected]:~>
        

        Now when I am trying to use put command, the output something below.

        AZA1UATXXAML004:ef>/opt/itm/data/dbextract/OneCertReport > lftp sftp://DTUIT98101UAT:qw^A48923%%[email protected]:10023
        lftp [email protected]:~> put Fortent_A_PROD.dsv
        put: Login failed: Login incorrect
        lftp [email protected]:~>
        

        Its not letting me to transfer the files

        • Ravi Saive says:

          @Chitvan,

          First try to connect to an SFTP session by using following command along with the username and IP address at the command prompt. Once authentication successful, you will see a shell with an sftp>; prompt.

          # sftp [email protected]
          

          Output would be something like.

          Connecting to 22.113.232.22...
          [email protected]'s password:
          sftp>
          

          Once you login, try to upload single file using put command as show.

          sftp> put file.txt
          
          • Chitvan says:

            Thanks Ravi for the reply, but still facing same issue:

            AZA1UATXXAML004:ef>/opt/itm/data/dbextract/OneCertReport > sftp [email protected]
            Connecting to 22.113.232.22...
            ssh: connect to host 22.113.232.22 port 22: Connection timed out
            Couldn't read packet: Connection reset by peer
            
            
            
            AZA1UATXXAML004:ef>/opt/itm/data/dbextract/OneCertReport > sftp [email protected]
            Connecting to 22.113.216.20...
            ssh: connect to host 22.113.216.20 port 22: Connection timed out
            Couldn't read packet: Connection reset by peer
            AZA1UATXXAML004:ef>/opt/itm/data/dbextract/OneCertReport >
            
          • Ravi Saive says:

            @Chitvan,

            I think SSH is giving some trouble here, I think you should first configure your SSH to accept sftp connections, read this article https://www.tecmint.com/restrict-sftp-user-home-directories-using-chroot/.

            Else, you could use proftpd which offers a SFTP frontend.

  5. Jay Cajigas says:

    Very nice! Works great!

    I do have a problem when downloading multiple files (hundreds). It exits the shell script before finishing to download all files.

    I have to manually connect and download everything (get inbox/*).

    Is there a way to get all files and then have the shell script exit?

  6. Anish Srivastava says:

    Perfect explaination

  7. Oliver Weiler says:

    Thank you very much, just what I needed!

  8. Raj says:

    How do i copy the files incrementally using sftp from a window server.

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.