

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.
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.
Table of Contents
1. Connect to the SFTP server
You can connect to a remote server via SFTP using the following command
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
Where ‘2345’ is the port number you are using.
2. 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.
You can also list the files on the local system
3. Navigate through directories
To find the current working directory on the local server use
for the local server, use the following command
You can easily change the working directory in SFTP. For the remote server use
and for the local server use
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.
To upload multiple files use the following command
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
5. Download files and directories using the get command
Download a single file from the remote to the local machine
or download multiple files with the ‘mget’ command
Download a directory and all its content with the following command
6. Create and remove directories
Create a new directory on the remote server
Create a new directory on the local server
7. Getting Help
You can find all SFTP commands with a short description by executing ‘help’ or ‘?’.
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.
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\]\u@\h\[\033[00m\]:\[\033[01;34m\] \w\[\033[01;37m\] > ‘
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.
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.
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 useMGET
.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.
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?
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.
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.