How to work with SFTP client in Linux – 10 sftp commands

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’.

# 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

# Table of content

  1. Connecting to SFTP server
  2. Checking sftp version
  3. Showing remote working directory
  4. Show local machine working directory
  5. Upload files to the sftp server
  6. Downloading files from the sftp server
  7. Creating and deleting directory in the sftp server
  8. Removing files in the sftp server
  9. Renaming files in the sftp server
  10. Checking file usage in the sftp server
  11. Getting help

# 1. 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:

sftp [user]@[host]

Lets connect to our server. your SFTP username and password will be needed.

sftp sftpuser1@192.168.10.10

Output:

$ sftp sftpuser1@192.168.10.10
The authenticity of host '192.168.10.10 (192.168.10.10)' can't be established.
ECDSA key fingerprint is SHA256:99KvuL95zO2CQbC8X0Re/Q+cYrJgqQgzpf1leemnjmY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.10.10' (ECDSA) to the list of known hosts.
sftpuser1@192.168.10.10's password:
Connected to 192.168.10.10.
sftp>

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.

# 2. Checking SFTP Version

Use the version command to check sftp version.

sftp> version
SFTP protocol version 3
sftp>

# 3. Show remote Working Directory

If you need to check the current working directory on the remote server, the pwd command comes in handy:

sftp> pwd
Remote working directory: /sftpuser1
sftp>

# 4. Show Local machine Working directory

To show the local system’s present working directory use lpwd command.

sftp> lpwd
Local working directory: /home/ubuntu
sftp>

The created user will only have access to its directory. Lets switch to that directory with the following command:

sftp> cd sftpuser1
sftp>

Move to the SFTP user home directory and try to create a new directory as following:

sftp> ls
sftpuser1  ubuntu
sftp> cd sftpuser1
sftp> ls
sftp> mkdir datadir
sftp> ls
datadir

# 5. Upload files to the sftp server

# Doing it from local

Upload files to a remote server using this command syntax:

$ sftp {user}@{host}:{remote-path} <<< $'put {local-path}'

Example:

$ sftp sftpuser1@127.0.0.1:/sftpuser1/ <<< $'put ./citizix.txt'
sftpuser1@127.0.0.1's password:
Connected to 127.0.0.1.
Changing to: /sftpuser1/
sftp> put ./citizix.txt
Uploading ./citizix.txt to /sftpuser1/citizix.txt
./citizix.txt
# Doing it from the sftp server

Use these commands: ls to list files, lls for local list, put for uploading files:

sftp>
sftp> pwd
Remote working directory: /sftpuser1/datadir
sftp> ls
sftp> lls
citizix.txt  snap  tmp
sftp> put citizix.txt
Uploading citizix.txt to /sftpuser1/datadir/citizix.txt
citizix.txt                                                            100%   32    25.8KB/s   00:00
sftp> ls
citizix.txt
sftp>

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 data2data3 and ignore data1. You can use any wildcard or regular expression with mput.

sftp>
sftp> pwd
Remote working directory: /sftpuser1/data
sftp> ls
sftp> lls
data1  data2  data3
sftp> mput data[23]
Uploading data2 to /sftpuser1/data/data2
data2                                                                  100%    0     0.0KB/s   00:00
Uploading data3 to /sftpuser1/data/data3
data3                                                                  100%    0     0.0KB/s   00:00
sftp> ls
data2  data3

# 6. 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:

$ sftp {user}@{remote-host}:{remote-file-name} {local-file-name}

Here’s a demo of downloading a file in one line using sftp:

# sftp sftpuser1@127.0.0.1:/sftpuser1/citizix.txt .
sftpuser1@127.0.0.1's password:
Connected to 127.0.0.1.
Fetching /sftpuser1/citizix.txt to ./citizix.txt
/sftpuser1/citizix.txt                                                 100%   32    26.7KB/s   00:00
# Doing it from the remote sftp server

Download single file from the server using get. Example downloading data2

sftp>
sftp> ls
data2  data3
sftp> lls
sftp> get data2
Fetching /sftpuser1/data/data2 to data2
sftp> lls
data2
sftp>

Use mget to download multiple files like in this example

sftp>
sftp> ls
data2  data3
sftp> lls
sftp> mget data*
Fetching /sftpuser1/data/data2 to data2
Fetching /sftpuser1/data/data3 to data3
sftp> lls
data2  data3
sftp>

# 7. Creating and deleting directory in the sftp server

Use the commands mkdir and rmdir to create and delete directories

sftp>
sftp> ls
sftp> mkdir data
sftp> ls
data
sftp> rmdir data
sftp>

# 8. Removing files in the sftp server

Use the command rm to delete files

sftp> rm data*
Removing /sftpuser1/data/data2
Removing /sftpuser1/data/data3
sftp>

# 9. Renaming files in the sftp server

Use the rename command to rename files

sftp>
sftp> ls
data3
sftp> rename data3 data_original
sftp> ls
data_original
sftp>

# 10. 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.

sftp> df
        Size         Used        Avail       (root)    %Capacity
    29540600      5917856     22103188     23622744          20%
sftp> df -h
    Size     Used    Avail   (root)    %Capacity
  28.2GB    5.6GB   21.1GB   22.5GB          20%

# 11. Getting Help

To get help about available commands and syntax for SFTP, use ‘?‘ or ‘help‘.

sftp> ?

Output:

sftp> ?
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp [-h] grp path                Change group of file 'path' to 'grp'
chmod [-h] mode path               Change permissions of file 'path' to 'mode'
chown [-h] 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 [-afpR] remote [local]         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 [-afpR] local [remote]         Upload file
pwd                                Display remote working directory
quit                               Quit sftp
reget [-fpR] remote [local]        Resume download file
rename oldpath newpath             Rename remote file
reput [-fpR] local [remote]        Resume upload 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

Refer sftp man page for further reading.

$ man sftp
# Exit the SFTP Session

Finally, you can exit the sftp using the byeexit, or quit:

sftp> exit
$

# Conclusion

In this guide we managed explore basic operations that can be done on an SFTP server

Last updated on Mar 20, 2024 17:19 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy