
sftp Command Tutorial
The sftp (Secure File Transfer Protocol) command is a powerful and secure method for transferring files between local and remote systems over a network. It operates over the SSH (Secure Shell) protocol, ensuring that both commands and data are encrypted, providing a secure alternative to the traditional FTP (File Transfer Protocol). This makes it particularly useful for transferring sensitive information or working within environments that require robust security measures.
Table of Contents
Here is a comprehensive guide to the options available with the sftp command â
Syntax of sftp Command
The general syntax for the sftp command is −
sftp [options] [user@]host[:file ...]
Where −
- [options] − Specifies the various options and flags that control the behavior of sftp.
- [user@]host − Represents the remote user and hostname or IP address of the remote system.
- [:file ...] − Optionally specifies the files to be transferred or operations to be performed.
sftp Command Options
Option | Description |
---|---|
-4 | Forces sftp to use only IPv4 addresses. |
-6 | Forces sftp to use only IPv6 addresses. |
-A | Enables forwarding of the SSH authentication agent to the remote system. |
-a | Attempts to resume interrupted file transfers instead of overwriting existing files. |
-B buffer_size | Sets the buffer size used by sftp for file transfers. |
-b batchfile | Reads a list of commands from a batch file instead of standard input. |
-C | Enables compression for data transfer. |
-c cipher | Chooses a specific cipher for encrypting the data transfer. |
-D sftp_server_command | Connects directly to a local sftp server without using SSH. |
-F ssh_config | Uses an alternative SSH configuration file. |
-f | Flushes files to disk immediately after transfer. |
-i identity_file | Specifies the private key file for public key authentication. |
-J destination | Connects to the target host through a jump host. |
-l limit | Limits the bandwidth for file transfers, specified in Kbit/s. |
-N | Disables quiet mode. |
-o ssh_option | Passes options to SSH in the same format as in the ssh_config file. |
-P port | Specifies the port number to connect to on the remote host. |
-p | Preserves file modification times, access times, and modes during transfers. |
-q | Enables quiet mode, suppressing progress meter and other messages. |
-R num_requests | Specifies the number of outstanding requests allowed at one time. |
-r | Recursively copies entire directories. |
-S program | Specifies the program to use for the encrypted connection. |
-s subsystem | sftp_server | Specifies the SSH2 subsystem or the path to the sftp server on the remote host. |
-v | Enables verbose mode, providing detailed log messages. |
-X sftp_option | Specifies options to control sftp protocol behavior. |
Examples of sftp Command in Linux
Let's explore a few practical examples of Linux sftp command −
- Connecting to a Remote Server
- Uploading File to a Remote Server
- Downloading a File from Remote Server
- Recursively Downloading a Directory
- Listing Files on a Remote Server
Connecting to a Remote Server
To connect to a remote server using sftp, use −
sftp user@hostname
This command initiates an sftp session with the specified remote server. Replace user with your username and hostname with the remote server's address.

Note − To navigate to different directories while using SFTP, you can use −
lcd /path/to/local/directory
Replace /path/to/local/directory with the actual path to the desired directory. For example −
lcd /Users/perfect/Documents
Uploading a File to a Remote Server
To upload a file to a remote server, you can use −
sftp user@hostname put localfile.txt remotefile.txt
This uploads localfile.txt from your local system to the remote server as remotefile.txt.

Downloading a File from a Remote Server
To download a file from a remote server, the following command can be used −
sftp user@hostname get remotefile.txt localfile.txt
This command downloads remotefile.txt from the remote server to your local system as localfile.txt.

Recursively Downloading a Directory
To recursively download an entire directory from a remote server, use −
sftp user@hostname get -r remote_directory local_directory
This downloads the entire remote_directory from the remote server to your local system as local_directory.

Listing Files on a Remote Server
To list files and directories on a remote server, the command is −
sftp user@hostname ls
This displays the contents of the current directory on the remote server.

Conclusion
The sftp command in Linux is a robust and secure tool for transferring files between local and remote systems. By understanding its purpose, syntax, options, and practical usage scenarios, you can effectively manage file transfers with confidence.
Whether you need to upload or download files, list remote directories, or perform batch operations, mastering the sftp command provides a reliable and secure solution. Incorporating the sftp command into your workflow enhances your ability to manage files across different systems, ensuring secure and efficient file transfers.