15 Examples of SFTP command in Linux

SFTP or Safe File Switch Protocol is a safe distant file switch utility primarily based on File Switch Protocol (FTP).

FTP visitors is unencrypted and insecure, which is why it has largely been changed by SFTP.

By default, SFTP runs over the SSH protocol on TCP port 22 and gives the identical set of safety and encryption capabilities as SSH. The usual SSH daemon that runs as a part of the OpenSSH server on Linux methods helps primary features of the SFTP protocol by default, though separate devoted software program is accessible, corresponding to vsftpd which could be configured to get extra options and customizations.

On this article, we’ll focus on utilizing SFTP from the command line. I am utilizing an Ubuntu system, though the instructions talked about right here will work on any Linux system sftp consumer.

Earlier than you begin any instructions it’s best to know that SCP is getting out of date and alternatively it’s good to familiarize your self with the SFTP command. With SFTP you are able to do virtually something you do with SCP.

Copy information

SFTP can be utilized as an alternative to SCP (Safe Copy) in some supported utilization eventualities. An instance of that is utilization SCP to push or fetch information from a distant server without delay.

The syntax for add utilizing the SCP command is as follows:

$ scp {local-path} {consumer}@{remote-host}:{remote-path}

And for to obtain comparable:

$ scp {consumer}@{remote-host}:{remote-file-path} {local-path}

Equally, we are able to use the next sftp command syntax to add information to distant server:

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

Beneath you’ll be able to see one demo add information by way of sftp as a one-liner:

$ sftp [email protected]:/house/ftpuser/remote_test_dir <<< $'put /house/abhisheknair/new_file'
[email protected]'s password:
Linked to 192.168.1.231.
Altering to: /house/ftpuser/remote_test_dir
sftp> put /house/abhisheknair/new_file
Importing /house/abhisheknair/new_file to /house/ftpuser/remote_test_dir/new_file
/house/abhisheknair/new_file                                                           100%    9     7.2KB/s   00:00
$

Nasty to obtain a file from a distant server, use the command syntax under:

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

Here is a demo of downloading a file in a single line utilizing sftp:

$ sftp [email protected]:/house/ftpuser/new_file1 /house/abhisheknair/new_local_dir
[email protected]'s password:
Linked to 192.168.1.231.
Fetching /house/ftpuser/new_file1 to /house/abhisheknair/new_local_dir/new_file1
/house/ftpuser/new_file1                                                               100%   12     3.6KB/s   00:00
$

It’s also possible to alternate the SSH key for passwordless authentication.

Hook up with an SFTP server

To ascertain an SFTP connection, use sftp command with a username and the title or IP deal with of the distant host. For this to work, the default TCP port 22 should be open, or else explicitly specify the port used -oPort flag.

I’m connecting to an SFTP server with IP 192.168.1.231. The primary time you hook up with an SFTP server, you’ll be requested to substantiate the server fingerprint, corresponding to SSH. As soon as confirmed by ‘Sure‘ The connection continues and a consumer’s password is requested.

You’ll be proven on profitable connection sftp> quick.

$ sftp [email protected]
The authenticity of host '192.168.1.231 (192.168.1.231)' cannot be established.
ECDSA key fingerprint is SHA256:k90E28Pfnjoiq1svFw18dA2mazweoCmR5Hqi8SH0mj0.
Are you positive you wish to proceed connecting (sure/no/[fingerprint])? sure
Warning: Completely added '192.168.1.231' (ECDSA) to the listing of recognized hosts.
ftp[email protected]'s password:
Linked to 192.168.1.231.
sftp>

Examine model

You may examine the SFTP model utilizing model command at sftp quick.

sftp> model
SFTP protocol model 3
sftp>

Get some assist

To get assistance on obtainable instructions and syntax for SFTP, use ‘?‘ or ‘workers‘.

sftp> ?
Out there instructions:
bye Give up sftp
cd path Change distant listing 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] personal path Change proprietor of file 'path' to 'personal'
df [-hi] [path] Show statistics for present listing or
filesystem containing 'path'
exit Give up sftp
get [-afpR] distant [local] Obtain file
assist Show this assist textual content
liquid crystal display path Change native listing to 'path'
lls [ls-options [path]] Show native listing itemizing
lmkdir path Create native listing
ln [-s] oldpath newpath Hyperlink distant file (-s for symlink)
lpwd Print native working listing
ls [-1afhlnrSt] [path] Show distant listing itemizing
lumask umask Set native umask to 'umask'
mkdir path Create distant listing
progress Toggle show of progress meter
put [-afpR] native [remote] Add file
pwd Show distant working listing
stop Give up sftp
reget [-fpR] distant [local] Resume obtain file
rename oldpath newpath Rename distant file
reput [-fpR] native [remote] Resume add file
rm path Delete distant file
rmdir path Take away distant listing
symlink oldpath newpath Symlink distant file
model Present SFTP model
!command Execute 'command' in native shell
! Escape to native shell
? Synonym for assist
sftp>

Present workbook

When you’re related to a distant server, you’ll be able to view the present workbook of the distant system utilizing pwd command.

sftp> pwd
Distant working listing: /house/ftpuser
sftp>

To show the present utilization of the native system working listing lpwd command.

sftp> lpwd
Native working listing: /house/abhisheknair
sftp>

Listing information

You may view information within the distant working folder utilizing ls command.

sftp> ls
remote_file1     remote_file2     remote_file3     remote_test_dir
sftp>

Use to listing information within the native working listing lls command.

sftp> lls
bin  file1  file2  file3  lib  oci-scripts  sys_info.sh  take a look at.tgz  testdir
sftp>

Swap directories

Switching the distant workbook could be accomplished utilizing cd command. See instance under:

sftp> pwd
Distant working listing: /house/ftpuser
sftp> ls
remote_file1     remote_file2     remote_file3     remote_test_dir
sftp> cd remote_test_dir
sftp> pwd
Distant working listing: /house/ftpuser/remote_test_dir
sftp>

Use to change native workbooks liquid crystal display command. Beneath is an easy instance to indicate its use liquid crystal display.

sftp> lpwd
Native working listing: /house/abhisheknair
sftp> lls
bin  file1  file2  file3  lib  oci-scripts  sys_info.sh  take a look at.tgz  testdir
sftp> liquid crystal display testdir
sftp> lpwd
Native working listing: /house/abhisheknair/testdir
sftp>

Add information

Use to add one file put command. Watch me add regionally file1 to the distant workbook utilizing put down command. I can confirm it utilizing ls command that prints the contents of the distant workbook.

sftp> pwd
Distant working listing: /house/ftpuser
sftp> lpwd
Native working listing: /house/abhisheknair
sftp> ls
remote_file1     remote_file2     remote_file3     remote_test_dir
sftp> lls
bin  file1  file2  file3  lib  oci-scripts  sys_info.sh  take a look at.tgz  testdir
sftp> put file1
Importing file1 to /house/ftpuser/file1
file1                                                                                                                                                                          100%    6     6.0KB/s   00:00
sftp> ls
file1            remote_file1     remote_file2     remote_file3     remote_test_dir
sftp>

We will use to add a number of information without delay mput command as proven under. I exploit mput with an everyday expression sample file[23] what really uploads file2 And file3 and skips file1 as a result of it was already uploaded within the earlier step. You should use any wildcard or common expression mput.

sftp> pwd
Distant working listing: /house/ftpuser
sftp> lpwd
Native working listing: /house/abhisheknair
sftp> ls
file1            remote_file1     remote_file2     remote_file3     remote_test_dir
sftp> lls
bin  file1  file2  file3  lib  oci-scripts  sys_info.sh  take a look at.tgz  testdir
sftp> mput file[23]
Importing file2 to /house/ftpuser/file2
file2                                                                                                                                                                          100%    6     6.5KB/s   00:00
Importing file3 to /house/ftpuser/file3
file3                                                                                                                                                                          100%    6     5.3KB/s   00:00
sftp> ls
file1            file2            file3            remote_file1     remote_file2     remote_file3     remote_test_dir
sftp>

Obtain information

One file with SFTP could be downloaded utilizing get command. Here is an instance the place I downloaded it external_file4 utilizing sftp:

sftp> pwd
Distant working listing: /house/ftpuser/remote_test_dir
sftp> lpwd
Native working listing: /house/abhisheknair/testdir
sftp> ls
remote_file4
sftp> lls
file4
sftp> get remote_file4
Fetching /house/ftpuser/remote_test_dir/remote_file4 to remote_file4
/house/ftpuser/remote_test_dir/remote_file4                                                                                                                                     100%   13     5.2KB/s   00:00
sftp> lls
file4  remote_file4
sftp>

Use to obtain a number of information <robust>mget</robust> command. I obtain right here all of the information that match the sample external_file* within the distant workbook to my native workbook. I lastly use <robust>lls</robust> command to view the downloaded information.

sftp> pwd
Distant working listing: /house/ftpuser
sftp> lpwd
Native working listing: /house/abhisheknair/testdir
sftp> ls
remote_file1     remote_file2     remote_file3     remote_test_dir
sftp> lls
file4  remote_file4
sftp> mget remote_file*
Fetching /house/ftpuser/remote_file1 to remote_file1
/house/ftpuser/remote_file1                                                                                                                                                     100%   12     5.9KB/s   00:00
Fetching /house/ftpuser/remote_file2 to remote_file2
/house/ftpuser/remote_file2                                                                                                                                                     100%   13     5.8KB/s   00:00
Fetching /house/ftpuser/remote_file3 to remote_file3
/house/ftpuser/remote_file3                                                                                                                                                     100%   13     7.3KB/s   00:00
sftp> lls
file4  remote_file1  remote_file2  remote_file3  remote_file4
sftp>

Create a folder

A brand new folder could be created on a distant server utilizing mkdir command.

sftp> pwd
Distant working listing: /house/ftpuser
sftp> ls
file1            file2            file3            remote_file1     remote_file2     remote_file3     remote_test_dir
sftp> mkdir new_dir
sftp> ls
file1            file2            file3            new_dir          remote_file1     remote_file2     remote_file3     remote_test_dir
sftp>

Equally, if you wish to create a brand new listing within the native system’s present working listing from the sftp immediate, use lmkdir command.

sftp> lpwd
Native working listing: /house/abhisheknair
sftp> lls
bin  file1  file2  file3  lib  oci-scripts  sys_info.sh  take a look at.tgz  testdir
sftp> lmkdir new_local_dir
sftp> lls
bin  file1  file2  file3  lib  new_local_dir  oci-scripts  sys_info.sh  take a look at.tgz  testdir
sftp>

Delete listing

An empty distant folder could be deleted utilizing rmdir command. Please notice that if it isn’t empty you’re going to get an error.

sftp> pwd
Distant working listing: /house/ftpuser
sftp> ls
file1            file2            file3            new_dir          remote_file1     remote_file2     remote_file3     remote_test_dir
sftp> rmdir new_dir
sftp> ls
file1            file2            file3            remote_file1     remote_file2     remote_file3     remote_test_dir
sftp>

Delete file

An exterior file could be deleted utilizing rm command.

sftp> pwd
Distant working listing: /house/ftpuser
sftp> ls
file1            file2            file3            remote_file1     remote_file2     remote_file3     remote_test_dir
sftp> rm remote_file3
Eradicating /house/ftpuser/remote_file3
sftp> ls
file1            file2            file3            remote_file1     remote_file2     remote_test_dir
sftp>

Rename file

An exterior file may also be renamed simply utilizing rename command.

sftp> pwd
Distant working listing: /house/ftpuser
sftp> ls
file1            file2            file3            remote_file1     remote_file2     remote_test_dir
sftp> rename remote_file1 new_file1
sftp> ls
file1            file2            file3            new_file1        remote_file2     remote_test_dir
sftp>

File system utilization

To show statistics for the present listing or file system containing ‘path’, use df command. We will use -h flag to show statistics in a human-readable format. Please notice that the statistics proven pertain to the respective file system of the distant SFTP server and never the file system of the native machine.

sftp> df
        Measurement         Used        Avail       (root)    %Capability
    17811456      1845472     15965984     15965984          10%
sftp> df -h
    Measurement     Used    Avail   (root)    %Capability
  17.0GB    1.8GB   15.2GB   15.2GB          10%
sftp>

Shut the SFTP session

Use both one to finish the SFTP session bye, exit, or stop command. You’ll be returned to your OS immediate after exiting SFTP.

sftp> exit
$

Conclusion

SFTP is likely one of the greatest choices obtainable and is each safe and simple to make use of. It gives each CLI and GUI options and is supported throughout platforms. Refer sftp man web page for extra info.

$ man sftp

If you wish to study extra, take a look at this Udemy Linux Mastery course.

Rate this post
Leave a Comment