Tutorial using SFTP commands to transfer files

SFTP (SSH File Transfer Protocol) is a secure file protocol used to access, manage and transfer files over an encrypted SSH transfer.

When compared to traditional FTP protocols, SFTP provides all the functions of FTP, and is easier to configure.

different from perintah scpwhich only allows file transfer, command sftp It allows you to perform various operations on remote files and resume file transfers.

In this tutorial, we will learn how to use the command sftp.

before starting

To be able to transfer and manage files via SFTP, you must have write permissions on the remote system.

When transferring large files, it is recommended to run the sftp command inside a tmux screen or session.

The directory where you run the command sftp It is a local business directory.

SFTP connection being established

SFTP operates on a client-server model. SFTP is a subsystem of SSH and supports all SSH authentication mechanisms.

While traditional password authentication is set by default and is easy to use, if you connect to the server regularly via SSH/SFTP, it is recommended to generate SSH keys and set up a passwordless SFTP login.

To open an SFTP connection to a remote system, use the sftp command followed by the remote server’s username and IP address or domain name:

sftp [email protected]_ip_or_hostname

If you are using password authentication, you will be asked to enter the user password. Once connected, the remote server will display a confirmation message and an sftp prompt.

Connected to [email protected]_ip_atau_hostname.

If the SSH server is not on the default port 22, use the option oPort which defines an alternate port:

sftp -oPort=custom_port [email protected]_ip_atau_hostname

SFTP command set

Most SFTP commands are the same or identical to the commands you’ll use at the Linux shell prompt.

See also  A tutorial on using the last command in Linux Terminal

You can get a list of all available SFTP commands by typing help or symbol ?

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 in SFTP

After logging in to the remote server, the current working directory is the user’s home directory on the remote machine. You can check this by typing:

Remote working directory: /home/remote_username

To list files and directories, use the ls command:


To navigate to another directory, use the cd command. For example, the command to move to a directory /tmp :

cd /tmp

All of the above commands are used to navigate and work in remote locations. The Sftp prompt also provides commands to navigate the local machine, view information, and manage files.

For example, to print the local working directory, we can write:

cd lpwd
Local working directory: /home/local_username

How to transfer files using SFTP

SFTP allows you to securely transfer files between two devices.

If your computer has GUI access, it is actually easier to use a desktop SFTP client such as WinSCP or FileZilla to connect to a remote server to download or upload files. However, the sftp command is useful when you are working on both machines without a GUI (command line only).

How to download files with SFTP command

After logging in to the remote server, the current working directory is the remote user’s home directory.

When downloading a file with the command sftpthe file will be downloaded to the directory where you typed the command sftp.

To download a single file from a remote server, use the command get:

get namafile.zip

The output will look like this:

Fetching /home/remote_username/namafile.zip to namafile.zip
/home/remote_username/namafile.zip                           100%   24MB   1.8MB/s   00:13

If you want to save the downloaded file with a different name, say the name after it:

get namafile.zip namafilebaru.zip

To download a directory from a remote system, use the options -r recursion:

get -r remote_directory

If the file transfer fails, you can continue to use the command reget. syntax reget same grammar get:

reget namafile.zip

How to upload files with SFTP command

To upload files from a local directory to a remote FTP server, use the command put:

put namafile.zip

The output will look like this:

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

If you want to load a file that is not in the current working directory, use the absolute path of the file.

See also  How to delete a user on Linux using the userdel command

When using the command putyou can use the same options available with the command get.

To copy a local directory, use the following command:

put -r direktori_lokal

To resume interrupted downloads:

reput filename.zip

Server management using SFTP

SFTP allows you to perform some basic file processing commands. Here are some examples of how to use an SFTP wrapper:

  • Displays remote system disk usage statistics:
            Size         Used        Avail       (root)    %Capacity
        20616252      1548776     18002580     19067476           7%
  • Create a new directory on the remote server:
    mkdir nama_direktori
  • Rename files on the remote server:
    rename nama_file nama_baru
  • Delete files on the remote server:
    rm nama_file
  • Delete the directory on the remote server:
    rmdir nama_direktori
  • Change file permissions on the remote system:
    chmod 644 nama_file
  • Change the owner of the file on the remote system:
    chown user_id nama_file

    You have to give the user ID of the command chown And the chgrp.

  • Change the owner of the remote file group with:
    chgrp group_id file_name

When you are done working, you can close the connection simply by typing bye or quit.


In this tutorial, you learned how to use the sftp command to download and upload files to a remote SFTP server.

If you regularly connect to the same system, you can simplify your workflow by specifying all connections in your SSH configuration file.

Source link