Quick take: sftp connects to a remote server over SSH and gives you an interactive prompt to browse directories, upload files with put, and download with get. Use sftp -P port user@host for non-standard ports.
Introduction
sftp (Secure File Transfer Protocol) is an interactive file transfer program that runs over SSH. Unlike scp which is a one-shot copy tool, sftp opens a persistent session where you can browse the remote filesystem, manage files, and transfer in either direction — all with SSH encryption.
It is widely supported and the correct choice when you need to explore a remote server's directory structure before deciding what to transfer, or when transferring multiple files interactively.
Syntax and Connection
# Connect to a remote server
sftp user@hostname
# Connect to a non-standard SSH port (capital P)
sftp -P 2222 user@hostname
# Connect using a specific SSH key
sftp -i ~/.ssh/deploy_key user@hostname
# Connect using SSH config alias
sftp myserverAfter connecting, you see a prompt: sftp>. The local and remote filesystems are both available. Commands prefixed with l (e.g. lls, lpwd) operate on the local side.
Navigating Directories
# Show current remote directory
sftp> pwd
# Show current local directory
sftp> lpwd
# List remote directory
sftp> ls -la
# List local directory
sftp> lls
# Change remote directory
sftp> cd /var/www/html
# Change local directory
sftp> lcd ~/Downloads
# Create a remote directory
sftp> mkdir uploads
# Remove a remote file
sftp> rm old_file.txtUploading Files
# Upload a single file to remote current directory
sftp> put local_file.txt
# Upload and rename on the remote side
sftp> put local_file.txt remote_name.txt
# Upload recursively (entire directory)
sftp> put -r ./my_project/
# Upload preserving timestamps and permissions
sftp> put -p config.confDownloading Files
# Download a single file to local current directory
sftp> get remote_file.txt
# Download and rename locally
sftp> get remote_file.txt local_copy.txt
# Download recursively (entire directory)
sftp> get -r /var/backups/weekly/
# Download preserving timestamps
sftp> get -p database_dump.sqlCommon Options
| Option | Description |
|---|---|
| -P port | Specify SSH port (capital P) |
| -i identity_file | Use a specific private key |
| -r | Recursive — transfer directories |
| -p | Preserve file timestamps and permissions |
| -b batchfile | Run commands from a file (batch mode) |
| -l limit | Limit bandwidth in kbps |
| -C | Enable compression for slow connections |
| -v | Verbose — debug connection issues |
sftp vs scp
scp is a one-shot tool: specify source and destination on the command line, and it copies then exits. It is faster for simple, known transfers because there is no session overhead.
sftp is interactive: connect once, then navigate and transfer multiple files across the session. Use sftp when you need to browse the remote server first, or when transferring many files in different locations.
For scripted, non-interactive transfers between well-known paths, scp is simpler. For anything that requires exploration or multiple operations, sftp wins.
Batch Mode and Automation
sftp can run commands from a file using -b, which enables basic automation:
# Create a batch command file
cat > upload_commands.txt <Batch mode exits with an error code if any command fails, making it suitable for use in shell scripts. For more complex automation over SSH, consider rsync which handles incremental sync and is more script-friendly.
Final Thoughts
sftp is the right tool when you need an interactive file transfer session over SSH — browsing directories, uploading, downloading, and managing files without writing a full rsync command. Learn the handful of interactive commands (ls, cd, put, get) and you will handle the majority of remote file transfer scenarios with one tool.
FAQ: sftp Command in Linux
How do I upload a file with sftp?+
Connect with sftp user@host, navigate to the target directory with cd, then use put localfile.txt to upload. Use put -r directory/ to upload a whole folder recursively.
How do I download a file with sftp?+
Connect to the server, navigate with cd to the remote directory, then run get remotefile.txt to download it to your current local directory. Use get -r folder/ for recursive download.
What is the difference between sftp and scp?+
scp is a simple one-shot copy command. sftp is interactive — you connect and get a prompt where you can browse directories, upload, download, and manage files. sftp is more flexible for browsing unknown directory structures.
How do I use sftp with a non-standard SSH port?+
Use sftp -P 2222 user@host where 2222 is your custom port. Note the capital P — sftp uses -P unlike ssh which uses lowercase -p.
How do I use sftp with an SSH key?+
Run sftp -i ~/.ssh/id_rsa user@host to specify a key file. If your key is already loaded in ssh-agent or listed in ~/.ssh/config, sftp picks it up automatically.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support