Master Secure Copy (scp): Syntax, Options, and Real-World Examples
This guide explains how the scp command works as a secure copy tool built on SSH, covering its basic syntax, three main copy scenarios, essential options for encryption, compression, bandwidth control, and practical examples for transferring files and directories between local and remote hosts.
Introduction
The scp command stands for Secure Copy, combining the functionality of cp with SSH. It uses the SSH protocol (default port 22) to log into a remote host before copying files, ensuring both file data and passwords are encrypted during transfer. scp supports three copy modes:
Copy from local to remote.
Copy from remote to local.
Copy between two remote systems.
Basic Syntax
The syntax mirrors that of cp:
$ scp source destination sourcedenotes the file’s current location, and destination is where the file should be copied. Both can include a username and hostname, separated by a colon. $ scp user@host:foo.txt bar.txt When a username or hostname is omitted, the current local user and host are assumed. scp also respects SSH client configuration in .ssh/config, allowing host aliases. scp can copy multiple files in one command: $ scp source1 source2 destination Be aware that existing files at the destination will be overwritten without warning.
Usage Examples
(1) Local to Remote
# syntax
$ scp SourceFile user@host:directory/TargetFile
# example
$ scp file.txt [email protected]:/remote/directoryCopy an entire directory recursively:
# copy local "documents" directory to remote, creating the directory there
$ scp -r documents username@server_ip:/path_to_remote_directory
# copy the whole local directory to a remote directory
$ scp -r localmachine/path_to_the_directory username@server_ip:/path_to_remote_directory/
# copy all contents of a local directory to a remote directory
$ scp -r localmachine/path_to_the_directory/* username@server_ip:/path_to_remote_directory/(2) Remote to Local
# syntax
$ scp user@host:directory/SourceFile TargetFile
# example
$ scp [email protected]:/remote/file.txt /local/directoryCopy a remote directory recursively:
# copy a remote directory to a local path
$ scp -r username@server_ip:/path_to_remote_directory local-machine/path_to_the_directory/
# copy all contents of a remote directory to a local path
$ scp -r username@server_ip:/path_to_remote_directory/* local-machine/path_to_the_directory/
$ scp -r user@host:directory/SourceFolder TargetFolder(3) Remote to Remote
# syntax
$ scp user@host1:directory/SourceFile user@host2:directory/SourceFile
# example
$ scp [email protected]:/files/file.txt [email protected]:/filesThe command will prompt for passwords of both remote accounts and transfer data directly between the two remote hosts.
Configuration Options
-c cipher : Specify the encryption algorithm for data transfer.
$ scp -c blowfish some_file [email protected]:~-C : Enable compression during transfer.
$ scp -c blowfish -C local_file [email protected]:~-F ssh_config : Use a specific SSH configuration file.
$ scp -F /home/pungki/proxy_ssh_config Label.pdf [email protected]:/root-i identity_file : Specify a private key for authentication.
$ scp -vCq -i private_key.pem ~/test.txt [email protected]:/some/path/test.txt-l limit : Limit bandwidth (Kbit/sec). Example limits to 80 Kbit/s (~10 KB/s).
$ scp -l 80 yourusername@yourserver:/home/yourusername/* .-p : Preserve modification time, access time, and mode.
$ scp -p ~/test.txt [email protected]:/some/path/test.txt-P port : Specify the remote SSH port (useful when not 22).
$ scp -P 2222 user@host:directory/SourceFile TargetFile-q : Suppress progress meter. $ scp -q Label.pdf [email protected]:. -r : Recursively copy directories.
-v : Verbose output for debugging.
$ scp -v ~/test.txt [email protected]:/root/help2356.txtSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
