sftp

networkingLinux/Unix
The sftp command is one of the most frequently used commands in Linux/Unix-like operating systems. sftp The sftp (Secure File Transfer Protocol) command provides an interactive file transfer program with functionality similar to FTP but operates over an encrypted SSH connection. It offers secure file transfer capabilities with additional features like resuming transfers and directory operations.

Quick Reference

Command Name:

sftp

Category:

networking

Platform:

Linux/Unix

Basic Usage:

sftp [options] [arguments]

Common Use Cases

  • 1

    Interactive file transfer

    Transfer files securely with an interactive command-line interface

  • 2

    Remote file management

    Create, delete, and organize files on remote servers

  • 3

    Automated scripts

    Use batch mode for automated file transfers in scripts

  • 4

    Web development

    Upload and manage website files on hosting servers

Syntax

sftp [OPTION] [[user@]host[:file [file]]]

Options

Option Description
-1 Use protocol version 1 (deprecated)
-4 Force use of IPv4 addresses
-6 Force use of IPv6 addresses
-B buffer_size Specify buffer size for transfers (default: 32768 bytes)
-b batchfile Batch mode - read commands from the specified file
-C Enable compression
-D sftp_server_path Connect directly to a local sftp server (rather than via ssh)
-F ssh_config Specify an alternative SSH configuration file
-i identity_file Specify the file from which the identity (private key) is read
-l limit Limit bandwidth in Kbit/s
-o ssh_option Pass options to ssh in the format used in ssh_config
-P port Specify the port to connect to on the remote host
-p Preserve modification times, access times, and modes from the original file
-q Quiet mode - disable progress meter and warning/diagnostic messages
-R num_requests Specify the number of outstanding requests (default: 64)
-r Recursively copy entire directories
-S program Specify the path to the ssh program
-v Verbose mode - display debugging messages

Examples

How to Use These Examples

The examples below show common ways to use the sftp command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

sftp user@remote_host

Connect to a remote SFTP server.

sftp user@remote_host:file.txt

Connect to a remote server and immediately download the specified file.

sftp -P 2222 user@remote_host

Connect to a remote server using a non-standard SSH port (2222).

Advanced Examples:


# After connecting with sftp:
get file.txt             # Download a file
put local_file.txt       # Upload a file
get -r remote_dir/       # Download a directory recursively
put -r local_dir/        # Upload a directory recursively
ls                       # List remote files
lls                      # List local files
cd /remote/path          # Change remote directory
lcd /local/path          # Change local directory
mkdir newdir             # Create remote directory
rm file.txt              # Delete remote file
rmdir dir                # Delete remote directory
exit                     # Quit SFTP session

Common SFTP interactive commands after establishing a connection.

sftp -b batch_file.txt user@remote_host

Execute SFTP commands in batch mode from a file.

sftp -i ~/.ssh/private_key user@remote_host

Connect using a specific identity file (private key) for authentication.


# Example batch file content:
cd /remote/directory
get important_file.txt
put local_update.txt
ls -la

Example content for a batch file to be used with the -b option.

sftp -R 1024 user@remote_host

Specify a limit on the number of outstanding requests.

echo "get file.txt" | sftp user@remote_host

Pipe SFTP commands directly to the sftp program.

sftp -D sftp_log.txt user@remote_host

Enable debugging mode with output sent to the specified file.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Points:

  • SFTP (Secure File Transfer Protocol) is a secure replacement for FTP
  • SFTP operates over the SSH protocol, providing encryption and secure authentication
  • SFTP is interactive by default, with a command prompt for executing file operations
  • The protocol supports resuming interrupted transfers (unlike SCP)
  • SFTP allows for directory listing, file creation, and removal operations
  • Multiple file transfers can be queued in a single session

Common SFTP Interactive Commands:

  • get - Download files from the remote server
  • put - Upload files to the remote server
  • ls - List remote directory contents
  • cd - Change remote directory
  • pwd - Display remote working directory
  • mkdir - Create remote directory
  • rmdir - Remove remote directory
  • rm - Delete remote files
  • rename - Rename remote files
  • chmod - Change permissions of remote files
  • lls - List local directory contents
  • lcd - Change local directory
  • lpwd - Display local working directory
  • help - Display help information
  • bye/exit/quit - Exit SFTP session

Advantages Over SCP:

  • Support for resuming interrupted transfers
  • Interactive filesystem navigation
  • Ability to remove and create directories remotely
  • More FTP-like interface familiar to users transitioning from FTP
  • Can be scripted using the batch mode
  • Directory listing capabilities without requiring full SSH access

Security Considerations:

  • Use SSH keys instead of passwords for enhanced security
  • Keep your SSH client and server software updated
  • Consider using a non-standard SSH port for added security
  • Use the -l option to limit bandwidth if needed
  • SFTP can be restricted to specific directories (chroot) on the server side
  • Disable unused SSH protocol versions, particularly version 1

Common Use Cases:

  • Web development - uploading files to web servers
  • System administration - transferring configuration files securely
  • Regular backups - securely transferring backup files
  • File sharing - secure alternative to FTP for sharing files
  • Automated file transfers - using batch mode for scripted operations
  • Remote file management - when full shell access is not needed or desired

Related Commands:

  • ssh - Secure shell protocol for remote login
  • scp - Secure copy (non-interactive file transfers)
  • rsync - Fast, versatile file copying tool
  • ftp - Traditional (insecure) file transfer protocol
  • sshfs - Mount remote filesystems using SSH

Tips & Tricks

1

Use batch mode for automation: sftp -b commands.txt user@host executes commands from a file

2

Create useful aliases in ~/.ssh/config: define Host shortcuts with custom settings

3

Resume interrupted transfers: use reget instead of get to resume a download

4

Use command history: press up/down arrows to navigate through previous commands

5

Enable compression: sftp -C user@host for faster transfers of text files

6

Set custom port: sftp -P 2222 user@host for non-standard SSH ports

7

List local files with lls: use lls to view local directory contents

8

Transfer entire directories: put -r local_dir or get -r remote_dir for recursive transfers

9

Use tab completion: press Tab to complete filenames and paths

10

Change permission: use chmod 644 file.txt to modify remote file permissions

Common Use Cases

Interactive file transfer

Transfer files securely with an interactive command-line interface

Remote file management

Create, delete, and organize files on remote servers

Automated scripts

Use batch mode for automated file transfers in scripts

Web development

Upload and manage website files on hosting servers

Resumable transfers

Safely resume interrupted downloads of large files

Related Commands

scp

scp

View command

rsync

rsync

View command

ssh

ssh

View command

ftp

ftp

View command

cp

cp

View command

curl

curl

View command

wget

wget

View command

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the sftp command works in different scenarios.

$ sftp
View All Commands