sftp

Secure file transfer over SSH connections

Syntax

sftp [options] [user@]host[:path]

Basic Usage

Connect to remote server

sftp username@hostname

Establishes an SFTP connection to the specified host.

Connect with specific port

sftp -P 2222 username@hostname

Connect using SSH key

sftp -i ~/.ssh/id_rsa username@hostname

Connect and change to specific directory

sftp username@hostname:/path/to/directory

Common Options

  • -1: Use SSH protocol version 1
  • -2: Use SSH protocol version 2
  • -4: Force IPv4
  • -6: Force IPv6
  • -B buffer_size: Set buffer size
  • -b batchfile: Execute commands from batch file
  • -C: Enable compression
  • -D sftp_server_path: Connect to sftp server
  • -F ssh_config: Use alternative SSH config file
  • -i identity_file: Use specific SSH key
  • -l limit: Limit bandwidth usage
  • -o ssh_option: Pass options to SSH
  • -P port: Connect to specific port
  • -R num_requests: Number of requests to buffer
  • -S program: Program to use for encrypted connection
  • -s subsystem: SSH2 subsystem or path
  • -v: Verbose mode

SFTP Commands

File Transfer Commands

  • put: Upload local file to remote server
  • get: Download remote file to local machine
  • mput: Upload multiple files
  • mget: Download multiple files
  • reput: Resume interrupted upload
  • reget: Resume interrupted download

File Management Commands

  • ls: List remote files
  • lls: List local files
  • mkdir: Create remote directory
  • lmkdir: Create local directory
  • rm: Remove remote file
  • lrm: Remove local file
  • rename: Rename remote file
  • lrename: Rename local file

Practical Examples

Basic file upload

sftp> put localfile.txt

Upload with different name

sftp> put localfile.txt remotefile.txt

Download file

sftp> get remotefile.txt

Upload multiple files

sftp> mput *.txt

Download multiple files

sftp> mget *.log

Change remote directory

sftp> cd /remote/directory

Change local directory

sftp> lcd /local/directory

Create remote directory

sftp> mkdir newdirectory

Remove remote file

sftp> rm oldfile.txt

View remote file permissions

sftp> ls -la

Exit SFTP session

sftp> quit

Batch Mode

Using Batch Files

Create a text file with SFTP commands and execute them automatically:

# batch.txt
cd /remote/directory
put file1.txt
put file2.txt
quit

Execute with:

sftp -b batch.txt username@hostname

Best Practices

When to Use

  • Secure file transfers over untrusted networks
  • Automated file transfer scripts
  • Transferring sensitive data
  • Cross-platform file transfers
  • Remote file management

Important Notes

  • SFTP requires SSH access to the remote server
  • Use key-based authentication for automated transfers
  • Large files may take time to transfer
  • Consider using rsync for large directory synchronization
  • SFTP is slower than FTP due to encryption overhead
  • Always verify file integrity after transfer

See Also

  • scp - Secure copy over SSH
  • rsync - Remote file synchronization
  • ssh - Secure shell connection
  • wget - Download files from web