Linux sftp Command

SSH File Transfer Protocol - 10 Practical Examples with Detailed Explanations

Example 1

Connect to Remote SFTP Server

$ sftp user@remote_host
Connected to remote_host. sftp>
Establishes an interactive SFTP session with a remote server. Replace 'user' with your username and 'remote_host' with the server address. Once connected, you'll see the 'sftp>' prompt where you can execute SFTP commands.
Interactive Mode: All subsequent commands are entered at the sftp> prompt
Example 2

Connect Using Custom Port

$ sftp -P 2222 user@remote_host
Connecting to remote_host... Connected to remote_host. sftp>
Connects to an SFTP server using a custom SSH port with the '-P' option (uppercase). Default SSH/SFTP port is 22, but many servers use custom ports for security. Always use uppercase -P for sftp.
Remember: sftp uses -P (uppercase) for port, unlike ssh which uses -p (lowercase)
Example 3

Upload File to Remote Server

sftp> put localfile.txt
Uploading localfile.txt to /home/user/localfile.txt localfile.txt 100% 2048KB 2.0MB/s 00:01
Uploads a file from your local machine to the remote server using the 'put' command. The file is transferred to the current remote directory. You can specify a different remote path as a second argument.
Specify Path: put localfile.txt /remote/path/newname.txt
Example 4

Download File from Remote Server

sftp> get remotefile.txt
Fetching /home/user/remotefile.txt to remotefile.txt /home/user/remotefile.txt 100% 1024KB 1.0MB/s 00:01
Downloads a file from the remote server to your local machine using the 'get' command. The file is saved to your current local directory. You can specify a different local path as a second argument.
Rename on Download: get remotefile.txt localname.txt
Example 5

Upload Directory Recursively

sftp> put -r local_directory
Uploading local_directory/ to /home/user/local_directory Entering local_directory/ local_directory/file1.txt 100% 512KB 512.0KB/s 00:01 local_directory/file2.txt 100% 256KB 256.0KB/s 00:01
Recursively uploads an entire directory and its contents to the remote server using the '-r' option with put. All subdirectories and files are transferred while preserving the directory structure.
Important: The remote directory will be created if it doesn't exist
Example 6

Download Directory Recursively

sftp> get -r remote_directory
Fetching /home/user/remote_directory/ to remote_directory/ Retrieving /home/user/remote_directory remote_directory/data.txt 100% 1024KB 1.0MB/s 00:01 remote_directory/logs.txt 100% 2048KB 2.0MB/s 00:01
Recursively downloads an entire directory from the remote server using the '-r' option with get. The complete directory structure is preserved locally, including all subdirectories and files.
Tip: Always verify available disk space before downloading large directories
Example 7

Navigate and List Directories

sftp> ls sftp> cd /var/www sftp> pwd sftp> lcd /home/local/path sftp> lpwd
file1.txt file2.txt directory1 Remote working directory: /var/www Local working directory: /home/local/path
Navigate and explore both remote and local filesystems. 'ls' lists remote files, 'cd' changes remote directory, 'pwd' shows remote path. Commands prefixed with 'l' (lcd, lpwd, lls) operate on your local system.
Remember: Commands without 'l' affect remote system, with 'l' affect local system
Example 8

Create and Remove Directories

sftp> mkdir new_directory sftp> rmdir empty_directory sftp> lmkdir local_directory
sftp> mkdir new_directory sftp> rmdir empty_directory
Create and remove directories on both remote and local systems. 'mkdir' creates remote directory, 'rmdir' removes empty remote directory, 'lmkdir' creates local directory. Use with caution when removing directories.
Note: rmdir only works on empty directories; use rm -r for non-empty ones
Example 9

Delete Remote Files

sftp> rm unwanted_file.txt sftp> rm *.log
Removing /home/user/unwanted_file.txt Removing /home/user/access.log Removing /home/user/error.log
Deletes files on the remote server using the 'rm' command. Supports wildcards for deleting multiple files matching a pattern. Be careful as this permanently removes files from the remote system.
Warning: Deletion is permanent! Always verify with 'ls' before using rm with wildcards
Example 10

Batch Mode with Command File

$ echo "cd /var/www get index.html put updated.css bye" > commands.txt $ sftp -b commands.txt user@remote_host
sftp> cd /var/www sftp> get index.html Fetching /var/www/index.html to index.html sftp> put updated.css Uploading updated.css to /var/www/updated.css sftp> bye
Executes SFTP commands from a batch file using the '-b' option, enabling automated file transfers without manual interaction. Perfect for scripts and scheduled tasks. Create a text file with one SFTP command per line.
Automation: Ideal for backup scripts and automated deployments
Bonus

Essential SFTP Commands Quick Reference

Connection: sftp user@host Connect to remote host bye / exit / quit Disconnect from server File Transfer: put file.txt Upload file get file.txt Download file put -r directory Upload directory recursively get -r directory Download directory recursively Navigation: ls / lls List remote / local files cd path / lcd path Change remote / local directory pwd / lpwd Show remote / local working directory File Operations: rm file.txt Remove remote file mkdir dirname Create remote directory rmdir dirname Remove empty remote directory rename old new Rename remote file Other: ! command Execute local shell command help / ? Display help
A comprehensive quick reference of the most commonly used SFTP commands for managing file transfers and navigation on both local and remote systems.