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-serversftp -P 2222 user@remote-serversftp user@remote-server:/remote/directory
Connecting to sftp
Basic Connection
Connect to a remote server:
sftp [email protected]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/documentsEssential sftp Commands
Navigation Commands
Remote Navigation
pwd- Show current remote directoryls- List remote directory contentsls -la- List with detailed informationcd directory- Change remote directorycd ..- Go to parent directory
Local Navigation
lpwd- Show current local directorylls- List local directory contentslls -la- List local files with detailslcd directory- Change local directory
File Transfer Commands
Download Files (Remote to Local)
Download a single file:
sftp> get filename.txtDownload with custom local name:
sftp> get remote-file.txt local-file.txtDownload to specific local directory:
sftp> get filename.txt /local/path/Download multiple files:
sftp> mget *.txt
sftp> mget file1.txt file2.txt file3.txtUpload Files (Local to Remote)
Upload a single file:
sftp> put localfile.txtUpload with custom remote name:
sftp> put local.txt remote.txtUpload to specific remote directory:
sftp> put localfile.txt /remote/path/Upload multiple files:
sftp> mput *.txt
sftp> mput file1.txt file2.txt file3.txtOther Useful sftp Commands
rm file- Delete a remote filemkdir directory- Create a remote directoryrmdir directory- Delete a remote directoryrename oldname newname- Rename a remote file or directorychmod permissions file- Change remote file permissionschown uid file- Change remote file ownerchgrp gid file- Change remote file grouphelp- Display sftp commandsexitorbye- 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 sftpTo open info page for sftp command we can use command below.
info sftpTo open help page from sftp command we can run command below.
sftp --helpsftp Command Source Code
You can find sftp command source code from the following repositories:
Related Linux Commands
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:
sftpprovides 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
scpbut 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.