sftp Command

What is Linux sftp Command?

sftp (SSH File Transfer Protocol) is an interactive command-line program for securely transferring files over SSH connections. Unlike scp which is designed for simple file copying, sftp provides a full interactive session with file management capabilities similar to traditional FTP but with SSH encryption. sftp runs over the SSH protocol and provides all the functionality of FTP with the security benefits of SSH. It allows you to navigate directories, list files, and perform various file operations on both local and remote systems within a single secure session. From the man page:

sftp is a file transfer program, similar to ftp, which performs all operations over an encrypted ssh transport. It may also use many features of ssh, such as public key authentication and compression.

Key Features

  • Secure transfer: All data is encrypted using SSH
  • Interactive session: Provides a command-line interface for file operations
  • Full file management: Create, delete, rename files and directories
  • Resume capability: Can resume interrupted transfers
  • Batch operations: Supports scripted operations
  • Cross-platform: Available on all Unix-like systems

sftp Syntax

sftp [options] [user@]hostname[:directory]

Basic connection patterns:

  • sftp user@remote-server
  • sftp -P 2222 user@remote-server
  • sftp user@remote-server:/remote/directory

Connecting to sftp

Basic Connection

Connect to a remote server:

Connect with Custom Port

Use a non-standard SSH port:

sftp -P 2222 [email protected]

Connect with SSH Key

Use a specific private key:

sftp -i ~/.ssh/my-key [email protected]

Connect to Specific Directory

Start in a specific remote directory:

sftp [email protected]:/home/user/documents

Essential sftp Commands

Remote Navigation

  • pwd - Show current remote directory
  • ls - List remote directory contents
  • ls -la - List with detailed information
  • cd directory - Change remote directory
  • cd .. - Go to parent directory

Local Navigation

  • lpwd - Show current local directory
  • lls - List local directory contents
  • lls -la - List local files with details
  • lcd directory - Change local directory

File Transfer Commands

Download Files (Remote to Local)

Download a single file:

sftp> get filename.txt

Download with custom local name:

sftp> get remote-file.txt local-file.txt

Download to specific local directory:

sftp> get filename.txt /local/path/

Download multiple files:

sftp> mget *.txt
sftp> mget file1.txt file2.txt file3.txt

Upload Files (Local to Remote)

Upload a single file:

sftp> put localfile.txt

Upload with custom remote name:

sftp> put local.txt remote.txt

Upload to specific remote directory:

sftp> put localfile.txt /remote/path/

Upload multiple files:

sftp> mput *.txt
sftp> mput file1.txt file2.txt file3.txt

Other Useful sftp Commands

  • rm file - Delete a remote file
  • mkdir directory - Create a remote directory
  • rmdir directory - Delete a remote directory
  • rename oldname newname - Rename a remote file or directory
  • chmod permissions file - Change remote file permissions
  • chown uid file - Change remote file owner
  • chgrp gid file - Change remote file group
  • help - Display sftp commands
  • exit or bye - Exit sftp session

sftp Command Manual / Help

We can use man and info command to see the manual page of sftp command. sftp command also have --help option to show list of options.

To open man page for sftp command we can use command below. To exit man or info page you can press q.

man sftp

To open info page for sftp command we can use command below.

info sftp

To open help page from sftp command we can run command below.

sftp --help

sftp Command Source Code

You can find sftp command source code from the following repositories:

You can read tutorials of related Linux commands below:

Summary

In this comprehensive tutorial, we’ve covered the essential aspects of using sftp for secure interactive file transfer. sftp provides a powerful and secure way to manage files on remote systems with full SSH encryption.

Key takeaways:

  • sftp provides an interactive session for secure file transfer over SSH
  • Supports both upload and download operations with resume capability
  • Offers full file management including navigation, permissions, and directory operations
  • Can be used interactively or with batch scripts for automation
  • More feature-rich than scp but requires interactive session management
  • Essential for system administrators who need secure file management capabilities

Visit our Linux Commands guide to learn more about using command line interface in Linux.