How to use the command `sftp` (with examples)

How to use the command `sftp` (with examples)

The sftp command is a secure file transfer program that allows you to interactively copy files between hosts over SSH. It provides a secure way to transfer files between local and remote systems. Unlike scp or rsync, sftp allows for an interactive command mode, which makes it convenient for performing file transfers.

Use case 1: Connect to a remote server and enter an interactive command mode

Code:

sftp remote_user@remote_host

Motivation: This use case is useful when you want to connect to a remote server and have an interactive session for transferring files. It allows you to browse and manage the files on the remote system from your local machine.

Explanation:

  • sftp: The command to initiate the interactive file transfer program.
  • remote_user: The username of the remote user you want to connect as.
  • remote_host: The hostname or IP address of the remote server.

Example output:

Connected to remote_host.
sftp>

Use case 2: Connect using an alternate port

Code:

sftp -P remote_port remote_user@remote_host

Motivation: This use case is useful when the remote server is listening on a non-default SSH port. You can specify the alternate port to establish the connection.

Explanation:

  • -P remote_port: The option to specify the alternate port for the SSH connection.
  • remote_port: The port number on which the remote server is listening.

Example output:

Connected to remote_host at port remote_port.
sftp>

Use case 3: Connect using a predefined host (in ~/.ssh/config)

Code:

sftp host

Motivation: This use case is useful when you have defined the connection details for a remote host in the ~/.ssh/config file. It allows you to easily connect to the host by providing its alias.

Explanation:

  • host: The alias or hostname defined in the ~/.ssh/config file.

Example output:

Connected to host.
sftp>

Use case 4: Transfer remote file to the local system

Code:

get /path/remote_file

Motivation: This use case is useful when you need to download a file from the remote system to your local machine. It retrieves the specified remote file and saves it in the current local directory.

Explanation:

  • get: The command to retrieve a remote file from the remote system.
  • /path/remote_file: The path of the remote file you want to download.

Example output:

Fetching /path/remote_file to local_file
/path/remote_file             100%  500KB  10.0MB/s   00:05    

Use case 5: Transfer local file to the remote system

Code:

put /path/local_file

Motivation: This use case is useful when you want to upload a file from your local machine to the remote system. It transfers the specified local file to the remote system.

Explanation:

  • put: The command to send a local file to the remote system.
  • /path/local_file: The path of the local file you want to upload.

Example output:

Uploading /path/local_file to remote_file
/path/local_file              100%  500KB  10.0MB/s   00:05    

Use case 6: Transfer remote directory to the local system recursively (works with put too)

Code:

get -R /path/remote_directory

Motivation: This use case is useful when you need to download a directory from the remote system to your local machine. The -R flag ensures that the remote directory is transferred recursively, including all its subdirectories and files.

Explanation:

  • get: The command to retrieve a remote file or directory from the remote system.
  • -R: The flag to transfer the directory recursively.
  • /path/remote_directory: The path of the remote directory you want to download.

Example output:

Fetching /path/remote_directory to local_directory
/path/remote_directory/file1.txt              100%    1KB   1.0KB/s   00:01    
/path/remote_directory/file2.txt              100%    2KB   2.0KB/s   00:01    
/path/remote_directory/subdirectory/file3.txt  100%    3KB   3.0KB/s   00:01

Use case 7: Get list of files on local machine

Code:

lls

Motivation: This use case is useful when you want to list the files in the current local directory. It provides a convenient way to view the files available on your local machine within the sftp session.

Explanation:

  • lls: The command to list the files in the current local directory.

Example output:

file1.txt
file2.txt
directory1

Use case 8: Get list of files on remote machine

Code:

ls

Motivation: This use case is useful when you want to list the files in the current remote directory. It allows you to view the files available on the remote system within the sftp session.

Explanation:

  • ls: The command to list the files in the current remote directory.

Example output:

file1.txt
file2.txt
directory1

Conclusion:

The sftp command provides a secure interactive way to transfer files between local and remote systems. It offers various use cases, such as connecting to a remote server, transferring files in both directions, and listing files on the local and remote machines. Understanding these use cases can help you effectively utilize the sftp command for your file transfer needs.

Related Posts

How to use the command 'roll' (with examples)

How to use the command 'roll' (with examples)

The ‘roll’ command allows users to simulate rolling dice. It can roll different numbers and types of dice, perform mathematical operations, and filter or modify the results in various ways.

Read More
How to Use the Command qm (with examples)

How to Use the Command qm (with examples)

QEMU/KVM Virtual Machine Manager is a command-line tool used for managing QEMU-based virtual machines on Proxmox Virtual Environment (PVE).

Read More
How to use the command rustup doc (with examples)

How to use the command rustup doc (with examples)

This article will guide you through the different use cases of the rustup doc command, which allows you to open the offline Rust documentation for the current toolchain.

Read More