Skip to content
🛠️ToolsShed

SFTP Commands Reference

Complete SFTP commands cheat sheet for file transfer over SSH.

27 commands

sftpConnection

Connect to SFTP server

Syntax:

sftp [user@]host[:path]

Example:

sftp user@example.com
sftp -PConnection

Connect on a specific port

Syntax:

sftp -P <port> user@host

Example:

sftp -P 2222 user@example.com
sftp -iConnection

Connect using identity/key file

Syntax:

sftp -i <keyfile> user@host

Example:

sftp -i ~/.ssh/id_rsa user@example.com
exit / quit / byeConnection

Close the SFTP connection

Syntax:

exit

Example:

exit
lsNavigation

List remote directory contents

Syntax:

ls [-l] [path]

Example:

ls -l /home/user
llsNavigation

List local directory contents

Syntax:

lls [path]

Example:

lls ~/Downloads
pwdNavigation

Print remote working directory

Syntax:

pwd

Example:

pwd
lpwdNavigation

Print local working directory

Syntax:

lpwd

Example:

lpwd
cdNavigation

Change remote directory

Syntax:

cd <path>

Example:

cd /var/www/html
lcdNavigation

Change local directory

Syntax:

lcd <path>

Example:

lcd ~/projects
getTransfer

Download file from remote to local

Syntax:

get <remote> [local]

Example:

get backup.tar.gz
get -rTransfer

Download directory recursively

Syntax:

get -r <dir> [local]

Example:

get -r /remote/dir ./local
putTransfer

Upload file from local to remote

Syntax:

put <local> [remote]

Example:

put index.html /var/www/
put -rTransfer

Upload directory recursively

Syntax:

put -r <dir> [remote]

Example:

put -r ./dist /var/www/
mgetTransfer

Download multiple files matching pattern

Syntax:

mget <pattern>

Example:

mget *.log
mputTransfer

Upload multiple files matching pattern

Syntax:

mput <pattern>

Example:

mput *.jpg
regetTransfer

Resume an interrupted download

Syntax:

reget <remote> <local>

Example:

reget bigfile.zip bigfile.zip
rmManagement

Remove remote file

Syntax:

rm <path>

Example:

rm /tmp/old.log
rmdirManagement

Remove remote directory

Syntax:

rmdir <path>

Example:

rmdir /tmp/olddir
mkdirManagement

Create remote directory

Syntax:

mkdir <path>

Example:

mkdir /var/www/uploads
renameManagement

Rename or move remote file

Syntax:

rename <old> <new>

Example:

rename old.txt new.txt
chmodManagement

Change remote file permissions

Syntax:

chmod <mode> <path>

Example:

chmod 644 index.html
chownManagement

Change remote file owner

Syntax:

chown <owner> <path>

Example:

chown www-data file.php
dfInfo

Show remote disk usage

Syntax:

df [-h]

Example:

df -h
statInfo

Show file attributes

Syntax:

stat <path>

Example:

stat /etc/passwd
versionInfo

Show SFTP protocol version

Syntax:

version

Example:

version
helpInfo

Show all available commands

Syntax:

help

Example:

help

About this tool

SFTP (SSH File Transfer Protocol) is a secure method for transferring files between computers over an encrypted SSH connection. Unlike older protocols such as FTP, SFTP provides end-to-end encryption, protecting your data from interception and ensuring that authentication credentials remain confidential. This reference guide compiles the most commonly used SFTP commands in one place, making it easier for developers, system administrators, and DevOps engineers to execute file operations without memorizing syntax.

Using SFTP from the command line, you can connect to remote servers, browse directory structures, upload and download files, and manage permissions—all through a secure tunnel. Whether you're deploying applications, backing up configurations, or transferring logs, SFTP commands like get, put, ls, and cd are fundamental tools in your workflow. The cheat sheet format allows you to quickly look up command syntax and options without switching between multiple documentation pages.

This tool is most valuable for anyone working with remote servers in production environments where security and reliability matter. DevOps teams, system administrators, and developers benefit from having a quick reference for SFTP syntax, especially when troubleshooting file transfer issues or onboarding new team members who need to learn secure file handling practices.

Frequently Asked Questions

Code Implementation

import subprocess
import os

def sftp_connect(host: str, user: str, port: int = 22, key_file: str = None) -> list[str]:
    """Build an sftp command to connect to a remote host."""
    cmd = ["sftp"]
    if port != 22:
        cmd.extend(["-P", str(port)])
    if key_file:
        cmd.extend(["-i", key_file])
    cmd.append(f"{user}@{host}")
    return cmd

def sftp_batch_commands(local_dir: str, remote_dir: str, files: list[str]) -> str:
    """Generate SFTP batch file commands for uploading multiple files."""
    lines = [f"cd {remote_dir}", f"lcd {local_dir}"]
    for f in files:
        lines.append(f"put {f}")
    lines.append("bye")
    return "\n".join(lines)

# Example: create a batch file for SFTP upload
batch = sftp_batch_commands("/local/uploads", "/remote/data", ["a.csv", "b.csv"])
print("SFTP batch commands:")
print(batch)

# Write to batch file and run
with open("/tmp/sftp_batch.txt", "w") as bfile:
    bfile.write(batch)
cmd = sftp_connect("example.com", "myuser") + ["-b", "/tmp/sftp_batch.txt"]
print("\nCommand:", " ".join(cmd))
# subprocess.run(cmd)  # Uncomment to actually run

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.