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 Usefull 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.
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.
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.
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
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 using the buttons below or simply leave a reply. Thanks.