How to Use SCP Command in Linux: Syntax and Examples
The
scp command in Linux securely copies files between local and remote systems using SSH. Use scp source destination where source and destination can be local paths or remote paths in the format user@host:/path.Syntax
The basic syntax of the scp command is:
- scp: The command to securely copy files.
- source: The file or directory you want to copy. It can be local or remote.
- destination: Where you want to copy the file to. It can also be local or remote.
- Remote paths use the format
user@host:/path/to/file.
bash
scp [options] source destination
Example
This example copies a file named example.txt from your local machine to the home directory of user alice on a remote server server.com. It demonstrates copying a local file to a remote location.
bash
scp example.txt alice@server.com:~/
Output
example.txt 100% 12KB 1.2MB/s 00:00
Common Pitfalls
Common mistakes when using scp include:
- Forgetting to specify the remote user and host correctly.
- Not having SSH access or correct permissions on the remote machine.
- Using incorrect paths, especially forgetting the colon
:after the host in remote paths. - Trying to copy directories without the
-roption.
Example of wrong and right usage:
bash
scp /path/to/dir alice@server.com:/remote/path # Wrong: copies directory without -r scp -r /path/to/dir alice@server.com:/remote/path # Right: copies directory recursively
Quick Reference
| Option | Description |
|---|---|
| -r | Copy directories recursively |
| -P port | Specify SSH port if not default 22 |
| -v | Verbose mode, shows detailed progress |
| -C | Enable compression for faster transfer |
| -i identity_file | Use a specific private key file for authentication |
Key Takeaways
Use
scp source destination to copy files securely between local and remote machines.Remote paths must include
user@host: before the file path.Use
-r option to copy directories recursively.Ensure you have SSH access and correct permissions on the remote system.
Use options like
-P to specify a custom SSH port or -C to enable compression.