• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Use Linux SFTP Command to Transfer Files

Written by Admin, Updated On November 21, 2020
sftp, terminal
How to Use Linux SFTP Command to Transfer Files

SFTP, or SSH File Transfer Protocol is a secure protocol to transfer and access files over a encrypted secure connection. This tutorial explains how to use sftp command in Linux.

As you know FTP is a popular method of transferring files but it’s insecure protocol that should only be used in limited cases or on networks you trust secure. SFTP provides all the functionality of FTP, and it is easier to setup.

Prerequisite#

  • You must have write permission on remote system to transfer files using SFTP.
  • It is advised to run the sftp command inside a screen or tmux session when you are transferring large files.

Make an SFTP connection#

By default, SFTP uses the SSH protocol to authenticate and establish a secure connection. Because of this, the same authentication methods are available that are present in SSH.

Although passwords authentication method is easy to use and set up by default, if you need to connect your server often we recommend you create SSH keys and set up a passwordless SFTP login.

To establish an SSH connection with remote system and open up and SFTP session, run the sftp command followed by the remote server username and the IP address or domain name:

sftp remote_username@server_ip

Your prompt will change to an sftp prompt once you are connected to the remote server.

Connected to remote_username@server_ip_or_hostname.
sftp>

If you have different SSH port then default 22, use the -oPort option to specify the alternate port:

sftp -oPort=custom_port remote_username@server_ip_or_hostname

SFTP Commands#

SFTP commands are very similar to the Linux shell prompt commands. To get a list of all available SFTP commands type, help or ?.

help
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
...
...
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell
?                                  Synonym for help

Navigating with SFTP#

By default, once you are logged in to the remote server, your current working directory is the remote user home directory. You can check that by typing:

pwd
Remote working directory: /home/remote_username

You can get list of the files and directories of current working directory using ls command:

ls

To go to another directory, use the cd command. For example, to navigate to the /var directory type:

cd /var

The sftp shell also provides commands for local navigation, information and file management. The local commands are prefixed with the letter l.

For example, to get the local working directory, type:

cd lpwd
Local working directory: /home/local_username

To list the contents of the current directory on the local machine:

lls

Navigate to another directory use lcd command:

lcd Desktop

Transferring Files with SFTP#

As we discussed above usign SFTP you can transfer files securely between two systems.

You can use a GUI SFTP client like WinSCP or FileZilla to connect to the remote server and manage files.

Downloading Files with the SFTP Command#

If we would like download files from our remote host, we can do so by issuing the following command:

To download the files from remote server using sftp, run following command:

get filename
Fetching /home/remote_username/filename to filename
/home/remote_username/filename     100%   5MB   1.2MB/s   00:05

If you want to save the downloaded file with a different name, specify the new name as the second argument:

You can save the downloaded file with a different name by specify new name as the second argument:

get filename new_filename

To download a directory and contents of it from the remote system, use the recursive -r option:

get -r remote_directory

If a file transfer fails or is interrupted, you can resume it using the reget command.

reget filename

Uploading Files with the SFTP Command#

To upload or transfer local files to remote host, use put command:

put filename

It should show output something like this:

Uploading filename to /home/remote_username/filename
filename 100% 12MB 1.7MB/s 00:06

To upload a whole local directory and it’s contents, you would type:

put -r locale_directory

The same flags that work with get apply to put command.

To resume an interrupted upload:

reput filename

File Manipulations with SFTP#

SFTP allows you to perform some basic file manipulation commands. Following are some examples to use the SFTP shell:

To Get information about disk usage of the remote system:

df
 Size         Used        Avail       (root)    %Capacity
20616252      1548776     18002580     19067476           7%

Create a new directory on the remote server:

mkdir directory_name

Rename a file on the remote server:

rename file_name new_file_name

Delete a file on the remote server:

rm file_name

Delete a directory on the remote server:

rmdir directory_name

Change the permissions of a file on the remote system:

chmod 644 file_name

Change the owner of a file on the remote system:

chown user_id file_name

You must supply the user ID to the chown and chgrp commands.

Change the group owner of a remote file with:

chgrp group_id file_name

Close the connection by typing bye or quit.

Conclusion#

In this tutorial we explained how to use sftp command to download and upload files to your remote SFTP server.

Do not hesitate to leave a comment if you have any question or feedback.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Set Up Passwordless SSH Login
Next Article   How to Use Linux Screen

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook