In this guide, we will learn how to do basic operations on an sftp server.
The File Transfer Protocol is a standard communication protocol used for the transfer of computer files from a server to a client on a computer network.
FTP isn’t popular today because it Lacks Security. When a file is sent using this protocol, the data, username, and password are all shared in plain text, which means a hacker can access this information with little to no effort. For data to be secure, you need to use an upgraded version of FTP like SFTP.
SFTP Secure File Transfer Protocol is a file transfer protocol that provide secure access to a remote computer to deliver secure communications. It leverages SSH - Secure Socket Shell and is frequently also referred to as ‘Secure Shell File Transfer Protocol’.
Related Content
- How to set up an SFTP server on Debian 11 Server
- Download Files from SFTP server Using a python script
- List, Upload and Download files from an SFTP Server using golang
- How to set up an SFTP server on OpenSUSE Leap 15.3 Server
- How to install and set up sftp server in Ubuntu 20.04
- How to set up an SFTP server on CentOS 8 /RHEL 8 Server
Prerequisites
To follow along, ensure that you have the following:
- An SFTP server to connect to. If you want to set up the server check the guides above
- Credentials to connect to the server
Connecting to SFTP server
You can use the command line terminal to test your login to the SFTP. This can even be done locally in the sftp server.
This is the format of the login command:
|
|
Lets connect to our server. your SFTP username and password will be needed.
|
|
Output:
|
|
This will log you in the server. The default directory is the /home
dir specified in the ChrootDirectory /home
directive in the ssh server configs.
Checking SFTP Version
Use the version command to check sftp version.
|
|
Show remote Working Directory
If you need to check the current working directory on the remote server, the pwd
command comes in handy:
|
|
Show Local machine Working directory
To show the local system’s present working directory use lpwd
command.
|
|
The created user will only have access to its directory. Lets switch to that directory with the following command:
|
|
Move to the SFTP user home directory and try to create a new directory as following:
|
|
Upload files to the sftp server
Doing it from local
Upload files to a remote server using this command syntax:
|
|
Example:
|
|
Doing it from the sftp server
Use these commands: ls
to list files, lls
for local list, put
for uploading files:
|
|
To upload multiple files in one go, we can use mput
command like in the example below. You can use mput
with regular expression like data[23]
to upload data2
, data3
and ignore data1
. You can use any wildcard or regular expression with mput
.
|
|
Downloading files from the sftp server
Doing it from the local machine
To download a file from a remote server, use the below command syntax:
|
|
Here’s a demo of downloading a file in one line using sftp:
|
|
Doing it from the remote sftp server
Download single file from the server using get
. Example downloading data2
|
|
Use mget
to download multiple files like in this example
|
|
Creating and deleting directory in the sftp server
Use the commands mkdir
and rmdir
to create and delete directories
|
|
Removing files in the sftp server
Use the command rm
to delete files
|
|
Renaming files in the sftp server
Use the rename
command to rename files
|
|
Checking Filesystem Usage in the sftp server
Display statistics for the current directory or filesystem containing ‘path’, use df command. We can use -h flag to show statistics in a human-readable format. Do note that the statistics shown are for the remote SFTP server’s respective filesystem and not the local machine’s filesystem.
|
|
Getting Help
To get help about available commands and syntax for SFTP, use ‘?’ or ‘help’.
|
|
Output:
|
|
Refer sftp man page for further reading.
|
|
Exit the SFTP Session
Finally, you can exit the sftp using the bye
, exit
, or quit
:
|
|
Conclusion
In this guide we managed explore basic operations that can be done on an SFTP server