Linux: copying files and directories with scp
Examples of copying files and directories between hosts with scp, and the common options.
scp copies files and directories between hosts over SSH. A location has the form [user@]host:path; a path without a host refers to the local machine.
Copy a file from a remote host to the local host
scp username@from_host:file.txt /local/directory/
Copy a file from the local host to a remote host
scp file.txt username@to_host:/remote/directory/
Copy a directory from a remote host to the local host
scp -r username@from_host:/remote/directory/ /local/directory/
Copy a directory from the local host to a remote host
scp -r /local/directory/ username@to_host:/remote/directory/
Copy a file between two remote hosts
scp username@from_host:/remote/directory/file.txt username@to_host:/remote/directory/
Common options
| Option | Description |
|---|---|
-r |
Copy directories recursively. Symbolic links found during the traversal are followed. |
-C |
Enable compression (passes -C to ssh). |
-l limit |
Limit the bandwidth used, in Kbit/s. |
-o ssh_option |
Pass an option to ssh, in the format used in ssh_config. |
-P port |
Port to connect to on the remote host (capital P). |
-p |
Preserve modification times, access times and modes of the source files. |
-q |
Quiet mode: disables the progress meter and the warning and diagnostic messages from ssh. |
-v |
Verbose mode: prints debugging messages about progress, connection, authentication and configuration. Useful for troubleshooting. |
Notes
- The host can be an IP address or a domain name. After you press Enter, scp asks for the SSH password, unless key-based authentication is set up.
- The same commands work on macOS in Terminal. On Windows, WinSCP offers a graphical alternative.
- If the target file already exists, scp overwrites its contents. Check the destination path before copying.