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 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 replacement for the FTP protocol – SFTP.
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 instead of FTP whenever possible. 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 list and explain some of the most popular and useful SFTP commands. They are great for beginners – easy to understand and useful for everyday server tasks.
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.
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
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
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