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

File Transfer Protocol (FTP) was a widely used protocol to transfer files or data remotely in an unencrypted format which is not a secure way to communicate.

As we all know that File Transfer Protocol is not at all secure because all transmissions happen 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 (Secure Copy) and SSH (Secure Shell) addresses this security ambiguity and added an encrypted secure layer while transferring data between remote computers.

[ You might also like: Best Command-Line FTP Clients for Linux ]

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 the SSH port (Secure SHell) globally as this would be a security breach. You can only open for specific IP from where you are going to transfer or manage files on the remote system or vice versa.

This article will guide you to 10 sftp command examples to use through the interactive command-line interface in the Linux terminal.

1. How to Connect to SFTP

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

[root@tecmint ~]# sftp [email protected]

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

2. Getting Help

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

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

4. Listing Files with sFTP

Listing files and directories in local as well as a remote system ftp server.

On Remote
sftp> ls
On Local
sftp> lls

5. Upload File Using sFTP

Put single or multiple files in remote system ftp server.

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

6. Upload Multiple Files Using sFTP

Putting multiple files on in remote system ftp server.

sftp> mput *.xls

6. Download Files Using sFTP

Getting single or multiple files in a 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 the same name. We can download remote files with a different name by specifying the name at the end. (This applies only while downloading the single file).

7. Switching Directories in sFTP

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 Using sFTP

Creating new directories on local and remote locations.

sftp> mkdir test
sftp> lmkdir Documents

9. Remove Directories Using sFTP

Remove directory or file in a remote system.

sftp> rm Report.xls
sftp> rmdir sub1

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

10. Exit sFTP Shell

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

sftp> !

[root@sftp ~]# 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 to some extent.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

56 Comments

Leave a Reply
  1. 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

    Reply
  2. 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

    Reply
  3. @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.

    Reply
  4. 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
    
    Reply
      • 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

        Reply
        • @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
          
          Reply
          • 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 >
            
  5. 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?

    Reply
  6. Hey,
    I have two questions.

    1. How can i rename more than one files?
    2. How can i cd with a direct path?

    For example, change from “/export/test” to “/import/test”.

    Reply
  7. what sftp command do i need to use to download only one file from Multiple files from sftp server?

    For ex: test_1.txt ,
    test_2.txt,
    test_3.txt,
    test_4.txt resides on sftp server. All I would need is download only one file (Any one file)?

    Reply
  8. I tried to push all folders as well into another server, I did sftp> put *, it transfers all files excluding folder, then i created a folder and tried to sent the files like sftp> put wp-admin/* wp-admin (still it is sending all files of wp-admin to wp-admin excluding all folders). I have tried sftp> put -r foldername and tried get -r as well but i get “invalid flag -r”. I am using terminal, what shall i do ?

    Reply
  9. Hi Ravi,
    I am trying to use mput command to sftp multiple files with regular expression with below command:

    mput file*

    But this is not working and i have also tried the mput you have given in this article and i am getting an error saying
    “*” not found

    I’m using ksh to run sftp! Please help

    Reply
    • @Balu,

      You should execute these commands in bash shell only, ksh shell have different switches I think, you need check man pages for this..

      Reply
  10. Can anyone brief me how MFT is different from SFTP.

    kindly share your valuable inputs on this as we want to migrate the file processing system from SFTP to MFT. Also suggest a few commands in MFT to transfer the files from different servers.

    Reply
  11. Very nice article. I was researching how to solve a problem with sftp and this article helped me greatly. Thanks.

    Regarding item #10, ‘Exist sFTP Shell’ probably should be ‘Exit sFTP Shell’.

    Reply
    • @Patrick,

      Thanks for finding this article helpful, and also thanks for notifying us about that typo, corrected in the writeup..

      Reply
  12. Good morning guys. I’m having an issue with sftp. For some reason, unknown at the moment, sftp just stopped being able to send multiple files at once. I have a bunch of txt files in my folder and I’m running AIX, I can manually put only one at a time, wild cards like *.*, filename.* or even *.txt are no longer accepted. I have to put the full file for sftp to send it. This has been working for 3 months now but just stopped 2 days ago. Does anyone have a clue as to what happened?

    Reply
  13. I can not seem to input non english characters, such as Å, Ä or Ö (swedish a and o characters), in the linux CLI sftp client. When I try to nothing is printed.

    ls cmd displays files and dirs with these characters just fine, but I can not type in the characters, so entering a dir (cd) or downloading (get) a dir/file with any non english character seems impossible.

    This issue does not exist during a standard ssh shell login, so the locale does not seem to be the problem. It seems to be within the sftp client itself.

    Anyone who has an idea?

    Reply
  14. Hi, As i run the ls command in sftp it closes the connection.
    So can you tell that how can i use the ls command in sftp?
    sftp> pwd
    Remote working directory: /incoming
    sftp> ls
    Connection closed

    Reply
    • @Alok,
      It seems that the user you’re connecting to remote sftp has proper permission to /incoming directory? Please check and confirm..

      Reply
      • Hi Ravi,
        Thanks for your reply.
        Yes. user has the proper permission. User is the member of a group which has chrootjail configuration. So the user can not move to another directory. Still user is not able to see the list of directories and files in his own directory.

        Reply
  15. I wanted to know if we have any way to enable the SFTP in ascii mode.I have read online that ascii mode in SFTP is supported sftp v4 onwards, can you please help me to find out as in how can I set the transfer mode to ascii in SFTP??

    Thanks a lot in advance.

    Reply
    • @Rathi,
      Yes it’s possible….use winscp or ftp client to do so…or you can use command line tricks as described in the article..

      Reply
  16. Thanks alot. This post helped me move lots of files remotely. I like the simplicity and the approach in whole article. Keep Up!

    Reply
  17. 1 Important thing you forget to include

    For those having SFTP/SSH on different port can use below

    sftp -oPort=3476 user@host

    Where 3476 is port number.

    Thanks

    Reply
  18. I want to transfer an entire folder from one linux machine to another linux machine.
    Please let me know the command for the same.

    Reply
  19. sftp will not pass along a password as a parameter and you cannot script reading the password. You must either manually enter the password or use RSA keys to bypass using passwords. Search for “sftp rsa key password” and you will find many examples of how to do this. (This drove me crazy when I was first learning sftp, I was used to scripting the password for ftp).

    Reply
    • ssh-keygen -t rsa
      cd .ssh
      ls
      u have show the public key
      scp publickey oracle@sys2:/tmp
      u have cp the torget file have 1777 permessions
      k u have go to another system
      cd .ssh
      ls
      cp /tmp/publickey authorized_keys
      service sshd restart
      go to sys1
      ssh sys2
      do not ask the passwd

      Reply
  20. Hi,

    Generally it will prompt to enter password if we enter SFTP command.
    Is it possible to enter the password into the SFTP command so that I do not see a prompt again asking for password.

    Could you please suggest the syntax,

    My SFTP command is like this, then it will ask for a password say ‘passboss’
    sftp -o StictHostKeyChecking=no -oProxyCommand=’/usr/bin/nc -abcdef.kk.lmmm:1080 %h %p’ [email protected] 22

    Could you please add the password in the above syntax so that it will not prompt again.

    eagerly awaiting ur reponse.

    Regards,
    Kamal

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.