- What is SFTP and how it Works?
- How to Create an SFTP Connection?
- Top SFTP commands
- How to Transfer Files in SFTP?
- Some Other Useful SFTP commands
What is SFTP? How Does SFTP Work?
SFTP (Secure File Transfer Protocol) is the safest file transfer protocol used to access, manage, and transfer files over an entirely secured SSH network.
You might have heard about the FTP protocol. The SFTP is likely similar to FTP but, more secured. SFTP is mainly used to transfer and access files over a secured transport whereas, in FTP, you can perform several tasks such as editing, deleting, managing, and transferring the files.
Unlike SCP, the SFTP allows users to access and manage files apart from only transferring them. However, this tutorial is all about the SFTP. So, we’ll walk you through all the possible tasks you can perform in SFTP. In this article, we’re gonna walk you through the Top useful SFTP Commands with examples that everyone should know before getting started on the Secure File Transfer Protocol (SFTP).
Before getting into the SFTP Commands, to connect to a remote server using SFTP, you must have the permissions open in the remote server. If you’re not able to connect to SFTP, you should contact your remote hosting provider.
How to Create an SFTP Connection
SFTP supports all authentication methods of SSH as it is a client-server model. It’s recommended to use SFTP to transfer heavy files.
To establish an SFTP connection on an SSH network, you’ll have to have an SSH client installed on your local system. For instance, I’m using PuTTY to establish an SFTP connection from my local server to the remote server.
- To connect with SFTP, open PuTTY and connect with your local server by entering the IP address or hostname.
- Type the username & password to log into your local server.
- Now you can establish an SFTP connection by using the following command:
- See the below image for instance where I’ve used my remote_username & IP Address.
- Type the password of the remote server and hit enter to get connected with SFTP. You’ll be able to see the sftp> prompt after a successful connection.
In some cases, the remote server stops listening to the default 22 port, in this case, you can try entering the port manually while establishing the connection by following the below command.
sftp -p custom_port remote_username@serverIP_or_hostname
Top SFTP Commands
- Well, there are hundreds of commands that you can use in SFTP. Most of the commands are likely similar to the Linux Shell Commands. You can use the help command to get the list of all available SFTP 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'
exit Quit sftp
- You can see your present remote working directory with the help of the pwd command.
Remote working directory: /home/Getintod
Apart from that, you can add l (Local) before any SFTP command to perform it on your local server. For example, use the lpwd command to view the local present working directory.
Local working directory: /home/learnher
Use the ls command to see the list of files & directories present inside the remote SFTP.
sftp> ls home/user/path
Similar to this, use the lls command to see the list of files & directories present inside the local server.
sftp> lls home/user/path
To browse through the directories, you can use the cd command.
sftp> cd home/user/path
similar to this, use the lcd command to change the present working directory of your local server.
sftp> lcd home/user/path
Transfer Files in SFTP
You can easily Download or Transfer a file (Remote to Local) in SFTP using the get command.
sftp> get filename.zip
The downloaded file will be stored inside the current local working directory. Moreover, you can download the file with a new name by using the following command.
sftp> get filename.zip Newname.zip
In some cases, download or transfer may get failed or be interrupted in between. You can resume the download by following the below command.
sftp> reget filename.zip
Now you know the commands to transfer a file from the remote server to your local server. Moreover, you can also transfer a file from your local server to the remote server by using the put command.
sftp> put filename.zip
The put command will only transfer files, to transfer a directory from the local to the remote server, you can add a recursive -r argument in your command.
sftp> put -r directory_name
Likewise, to resume an interrupted file, use the below command.
sftp> reput filename.zip
You can use all the arguments in the put command you used in the get command.
Some Other Useful SFTP Commands
There are some other important SFTP commands that you should be aware of. However, if you know the Linux Shell Commands then it’ll be easy for you as you can also use some Linux shell commands in SFTP.
- To get the information about the remote server :
- To change the file permission of a file on the remote server :
sftp> chmod 644 filename.php
- To create a new directory on the remote server :
sftp> mkdir directory_name
- To delete a file on the remote server :
sftp> rm filename.php
- To rename a file on the remote server :
sftp> rename filaname.php new_filename.php
- To delete an empty directory on the remote server :
sftp> rmdir directory_name
- Change the group owner of a remote file :
sftp> chgrp group_id file_name
- Change owner of the file on the remote server (Root) :
[root@test pwd]# chown user_id file_name
- To know your current SFTP version :
- To change the group & owner of a file/directory (Root) :
[root@test pwd]# chown username:groupname filename.php
[root@test pwd]# chown username:groupname directoryname
- Move all contents inside a directory to another directory (Move all files & directories inside a directory to one level up).
sftp> mv -v directoryName/* ~/home/user/path
Other than this, you cannot delete any directory filled with contents inside it. Also, SFTP doesn’t allow users to create new files. However, you can still delete the file if you want.
To close the SFTP connection, type the bye or quit command and hit the enter button to move back to your local server.
We hope now you got some strong knowledge about the SFTP commands and the use of SFTP commands. Apart from that, now you can easily transfer or download files from SFTP to the local server and local to the SFTP server.