Linux Command Reference ยท Networking
Linux scp and sftp commands
Reference for scp and sftp on Linux: copy files over SSH, transfer directories, use ports and keys, run sftp batch files, and know when rsync is the better tool.
scp and sftp move files over an SSH connection, so they use the same keys, ports and ~/.ssh/config entries as ssh. scp is a one-shot copy command that looks like cp with a host: prefix. sftp is an interactive session (or a scripted batch) with ls, get, put and similar commands.
Recent OpenSSH releases make scp use the SFTP protocol internally, but the command line is unchanged.
scp file.txt user@host:/tmp/
scp -r user@host:/var/log/app ./logs
sftp user@host
Try it
Without a network or a usable SSH client in our sandbox, transfers are read-only. The runnable example writes an sftp batch file and shows how scripts quote it, since batch files are what you automate.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Read-only: a typical interactive sftp session.
$ sftp deploy@web sftp> cd /var/www sftp> ls index.html assets sftp> get index.html sftp> put local.css assets/site.css sftp> bye
Syntax
scp [OPTIONS] SOURCE... TARGET sftp [OPTIONS] [USER@]HOST[:PATH]
| Option | Tool | Description |
|---|---|---|
-P PORT |
both | Port. It is a capital P, unlike ssh -p. |
-i FILE |
both | Private key. |
-r |
both | Recursive (directories). |
-C |
both | Compress during transfer. |
-p |
scp | Preserve times and modes. |
-q |
scp | Quiet; no progress meter. |
-l KBITS |
scp | Limit bandwidth in Kbit/s. |
-J HOST |
both | Jump host. |
-3 |
scp | Route a remote-to-remote copy through your machine. |
-b FILE |
sftp | Run commands from a batch file. |
-o KEY=VALUE |
both | Any ssh option. |
Inside sftp: ls, cd, pwd, get, put, mkdir, rm, chmod, lcd, lpwd, lls, !command (run locally), bye.
Returns: scp returns 0 on success and non-zero on any failure. sftp -b stops at the first failing command unless that command starts with -.
Usage notes
- Capital P for the port, lowercase
ppreserves times inscp. Mixing them up is the most common typo. - Remote paths are interpreted by the remote side. A colon after the host means remote; a path with a colon in its local file name needs
./in front. - Quote remote wildcards:
scp 'host:/logs/*.log' .makes the remote shell expand them, otherwise your local shell tries first. - No resume, no delta. Both copy whole files each time. For repeated or large transfers use
rsync -e ssh, which sends only the differences and can continue. - Overwrites silently.
scpreplaces existing target files without asking. - Trailing slashes and directories:
scp -r dir host:/tmp/creates/tmp/dir; with the target not existing, the semantics depend on whether it is a directory, so create it first. - sftp is better for browsing, for example to find the right file, change permissions or delete things;
scpis better in one-line scripts. - Security: both rely on SSH for encryption and host verification. Avoid
-o StrictHostKeyChecking=noin anything but throwaway tests. - Chroot-only accounts commonly allow
sftpbut not shell login or old-stylescp; usesftpthere.
Related
ssh,rsync,cp,ssh-keygen,tar- Lessons: SSH and Remote Work, File Sharing