SFTP Command Reference

SSH File Transfer Protocol commands for interactive secure file transfers

Connecting to Server

Basic Connection

# Connect to remote server
sftp [email protected]

# Connect with specific port
sftp -P 2222 [email protected]

# Connect with specific SSH key
sftp -i ~/.ssh/custom_key [email protected]

Establish SFTP connection to remote server

Non-Interactive Commands

# Execute single command
sftp [email protected] <<< "get /remote/file.txt"

# Execute multiple commands from file
sftp -b commands.txt [email protected]

# Batch mode with inline commands
echo -e "cd /remote/path\nget file.txt\nbye" | sftp [email protected]

Run SFTP commands non-interactively for automation

Navigation Commands

Remote Directory Operations

# Show current remote directory
pwd

# Change remote directory
cd /path/to/directory

# Go to parent directory
cd ..

# List remote directory contents
ls
ls -la

# List specific remote directory
ls /path/to/directory

Navigate and explore remote filesystem

Local Directory Operations

# Show current local directory
lpwd

# Change local directory
lcd /path/to/local/directory

# List local directory contents
lls
lls -la

# Create local directory
lmkdir new_directory

Navigate and manage local filesystem during session

File Transfer

Download Files (get)

# Download single file
get remote_file.txt

# Download to specific local path
get remote_file.txt /path/to/local/file.txt

# Download multiple files
mget *.txt
mget file1.txt file2.txt file3.txt

# Download with resume capability
reget partially_downloaded.zip

Download files from remote server to local machine

Upload Files (put)

# Upload single file
put local_file.txt

# Upload to specific remote path
put local_file.txt /remote/path/file.txt

# Upload multiple files
mput *.pdf
mput file1.doc file2.doc file3.doc

# Resume interrupted upload
reput partially_uploaded.zip

Upload files from local machine to remote server

Directory Transfer

# Download entire directory recursively
get -r /remote/directory

# Upload entire directory recursively
put -r /local/directory

# Preserve file attributes during transfer
get -P remote_file.txt
put -P local_file.txt

Transfer entire directories with all contents

File Management

Remote File Operations

# Rename/move remote file
rename old_name.txt new_name.txt

# Delete remote file
rm file_to_delete.txt

# Delete multiple remote files
rm *.tmp

# Create remote directory
mkdir new_directory

# Remove remote directory
rmdir empty_directory

Manage files and directories on remote server

Permissions and Ownership

# Change remote file permissions
chmod 644 file.txt
chmod 755 script.sh

# Change remote file owner
chown user:group file.txt

# Change remote file group
chgrp groupname file.txt

Modify file permissions and ownership on remote server

Symlinks

# Create symbolic link on remote
symlink target_file link_name

# Read symbolic link target
readlink symlink_name

# Show real path
realpath file_or_link

Create and manage symbolic links on remote server

Session Management

Session Commands

# Display help
help
?

# Show SFTP version
version

# Execute local shell command
!ls -la
!pwd

# Exit SFTP session
exit
bye
quit

Control and manage SFTP session

Transfer Settings

# Show current settings
progress

# Enable/disable progress meter
progress

# Set transfer buffer size
-B buffer_size

# Limit bandwidth (Kbit/s)
-l limit

Configure transfer behavior and monitoring

Batch Operations

Batch File Example

# Create batch file (sftp-commands.txt)
cd /remote/backups
lcd /local/backups
get -r latest/
cd /remote/logs
mget *.log
bye

# Execute batch file
sftp -b sftp-commands.txt [email protected]

Automate multiple SFTP operations using batch files

Script Integration

#!/bin/bash
# Automated backup download script

SFTP_HOST="backup-server.com"
SFTP_USER="backupuser"
REMOTE_DIR="/backups/daily"
LOCAL_DIR="/home/user/backups"

sftp ${SFTP_USER}@${SFTP_HOST} << EOF
cd ${REMOTE_DIR}
lcd ${LOCAL_DIR}
get -r $(date +%Y-%m-%d)/
bye
EOF

Integrate SFTP into shell scripts for automation

Common Use Cases

Website Management

# Upload website files
sftp [email protected]
> cd /var/www/html
> lcd ./dist
> put -r *
> chmod 644 *.html
> chmod 755 *.cgi
> bye

Deploy and manage website files on remote server

Backup Operations

# Download backups interactively
sftp [email protected]
> cd /backups
> ls -la
> get -r 2024-12-24/
> get database_backup.sql.gz
> bye

Retrieve backups from remote backup server

Log Collection

# Download application logs
sftp [email protected]
> cd /var/log/application
> lcd ./logs
> mget *.log
> mget error-*.txt
> bye

Collect log files from production servers

Advanced Features

Connection Options

# Use compression
sftp -C [email protected]

# Specify cipher
sftp -c aes256-ctr [email protected]

# Verbose mode for debugging
sftp -v [email protected]

# Disable strict host key checking (use with caution)
sftp -o StrictHostKeyChecking=no [email protected]

Configure connection parameters for security and performance

File Globbing and Patterns

# Download all matching files
mget *.pdf
mget backup-2024-*.tar.gz

# Upload with patterns
mput *.jpg
mput report-*.docx

# Case-insensitive matching (if supported)
mget *.[Ll][Oo][Gg]

Use wildcards and patterns for bulk operations

Disk Space Management

# Check remote disk space (using shell command)
!df -h

# Check local disk space
df -h  # (from local shell before connecting)

# Check file sizes before download
ls -lh /remote/path/largefile.zip

Monitor disk space before large transfers

Troubleshooting

Connection Problems

# Debug connection with verbose mode
sftp -vvv [email protected]

# Test SSH connectivity first
ssh [email protected]

# Verify SFTP subsystem
ssh [email protected] "echo $SHELL"

# Check if SFTP is enabled
ssh [email protected] "grep -i sftp /etc/ssh/sshd_config"

Diagnose and resolve connection issues

Transfer Errors

# Check remote permissions
ls -la filename

# Verify write permissions on remote directory
!ls -ld /remote/directory

# Resume interrupted transfer
reget filename  # for downloads
reput filename  # for uploads

# Check remote disk space
!df -h /remote/path

Resolve file transfer failures and interruptions

Performance Issues

# Enable compression for large files
sftp -C [email protected]

# Increase buffer size
sftp -B 32768 [email protected]

# Use faster cipher (less secure)
sftp -c arcfour [email protected]

# Limit bandwidth to avoid network congestion
sftp -l 1000 [email protected]  # 1000 Kbit/s

Optimize transfer speed and network usage

Quick Command Reference

CommandDescription
get fileDownload file from remote
put fileUpload file to remote
mget patternDownload multiple files matching pattern
mput patternUpload multiple files matching pattern
lsList remote directory
llsList local directory
cd pathChange remote directory
lcd pathChange local directory
pwdShow remote working directory
lpwdShow local working directory
mkdir dirCreate remote directory
rm fileDelete remote file
rename old newRename remote file
chmod mode fileChange remote file permissions
exitClose SFTP session