SFTP Command Line Examples

SFTP Command Line Examples
SFTP Command Line Examples

SFTP command examplesToday we will present you several  SFTP command line examples. We are all familiar with the FTP 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. Today we will present you several  SFTP command examples. Let’s get started.

linux sftp commandSFTP 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. SFTP Command Example #1 – Connect to the SFTP server

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

sftp  [email protected]_Address

[email protected]_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 [email protected]_Address

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

Linux SFTP command2. SFTP Command Line Example #2 – List all files in the current directory

Using the ls command you can list all files and directories in the current working directory

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

You can also list the files on the local system

sftp> lls

3. Linux SFTP Command Example #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. Linux SFTP Command Line Example #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. SFTP Command Example #5 – Download files and directories using the get command

Download a single file from the remote to the local machine

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. SFTP Command Example #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. SFTP Command Example #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 examplesOf 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 the social networks using the buttons below or simply leave a reply. Thanks.

9 thoughts on “SFTP Command Line Examples”

  1. Is there any way to change the default prompt “sftp>” to have color, for example? Thanks

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

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

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

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

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

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

    • mput is an undocumented alias to put. If you have issues with mput, you should consider using rsync over ssh.

  5. 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?

Leave a Comment

To prove you are human please solve the following *