Catalog / SFTP Command Cheat Sheet

SFTP Command Cheat Sheet

A quick reference guide for Secure File Transfer Protocol (SFTP) commands, covering basic operations, file management, permissions, and advanced usage.

Basic SFTP Commands

Connecting to a Server

sftp user@host

Connect to host as user.

sftp -P port user@host

Connect to host on a specific port as user. (Note: Capital -P is important.)

sftp -i identity_file user@host

Connect using the key specified by identity_file for authentication.

sftp host

Connect to host as current local user.

exit or bye

Terminate the SFTP session.

Navigating the SFTP Environment

pwd

Print working directory on the remote host.

lpwd

Print working directory on the local host.

cd path

Change remote directory to path.

lcd path

Change local directory to path.

ls

List files in the current remote directory.

lls

List files in the current local directory.

ls -l

Show more details about files in the current remote directory.

File Transfer Operations

get remote_file local_file

Download remote_file and save it as local_file.

get remote_file

Download remote_file and save it with the same name locally.

put local_file remote_file

Upload local_file and save it as remote_file.

put local_file

Upload local_file and save it with the same name remotely.

mget remote_files

Download multiple remote_files.

mput local_files

Upload multiple local_files.

File Management

Basic File Operations

mkdir directory_name

Create a new directory on the remote server.

rmdir directory_name

Remove a directory on the remote server (must be empty).

rm filename

Remove a file on the remote server.

rename old_path new_path

Rename a file or directory on the remote server.

ln -s target link_name

Create a symbolic link on the remote server.

File Permissions

chmod mode path

Change permissions of a file or directory on the remote server.

chown owner path

Change the owner of a file or directory on the remote server.

chgrp group path

Change the group of a file or directory on the remote server.

stat path

Display file or directory attributes.

File Transfer Options

reget remote_file local_file

Resume downloading remote_file to local_file.

reput local_file remote_file

Resume uploading local_file to remote_file.

set_transfer_option

Example: set progress true. Sets options for file transfers. Use help to see available options.

get -P remote_file local_file

Download with preservation of modification times, access times, and modes.

put -P local_file remote_file

Upload with preservation of modification times, access times, and modes.

Advanced SFTP Usage

Command Execution

!command

Execute a local shell command. Example: !ls -l.

version

Display the SFTP version.

help [command]

Get help on SFTP commands. If no command is given, it prints general help.

Batch Processing

sftp -b batch_file user@host

Run SFTP commands from a batch_file. Each line in the file is treated as an SFTP command.

Example batch file:

get remote_file local_file
put local_file remote_file
exit

sftp -b -

Read commands from standard input.

Configuration

~/.ssh/config

You can pre-define connection settings in your SSH configuration file. This allows you to use shorthand commands.

Example:

Host myserver
    HostName server.example.com
    User myuser
    Port 2222
    IdentityFile ~/.ssh/mykey

Then connect with: sftp myserver

Troubleshooting and Tips

Common Errors

Permission Denied:
Ensure you have the necessary permissions on both the local and remote systems. Check file and directory permissions, and that your SSH key (if used) is properly configured and authorized on the remote server.

Connection Refused:
Verify that the remote server is running and accessible on the specified port. Firewall rules may be blocking the connection.

File Not Found:
Double-check the spelling and path of the file or directory you are trying to access. Use pwd and lpwd to confirm your current locations.

SFTP Tips

Tab Completion:
Use the Tab key to auto-complete file and directory names. This can save time and prevent typos.

Wildcards:
Use wildcards (*, ?) with mget and mput to transfer multiple files matching a pattern. Be careful to avoid unintended matches.

Quoting:
If a file or directory name contains spaces or special characters, enclose it in quotes.

Batch Scripting:
For repetitive tasks, create an SFTP batch script to automate the process.