Scp command examples

The scp command lets you securely copy files between hosts over an SSH connection.

Some of you might think scp is deprecated but in fact what’s deprecated is the scp protocol, not the command itself. The command now uses a more secure sftp protocol by default

Here are some of useful scp command examples 😎👇 #linux #cybersecurity #privacy #dataprivacy #techtips

Find high-res pdf books with all my Linux and cybersecurity related infographics from https://study-notes.org

2025/11/12 Edited to

... Read moreThe SCP (Secure Copy) command is a powerful tool for securely transferring files over a network using SSH, and despite misconceptions about its deprecation, it remains widely used today with improved security based on the SFTP protocol. To get the most from SCP, it's essential to understand some practical command options. For example, to copy a file from local to remote, you use: ``` scp /path/to/local/file user@remotehost:/path/to/remote/directory ``` And for copying data back from remote to local: ``` scp user@remotehost:/path/to/remote/file /path/to/local/directory ``` Recursive directory copying can be done using the `-r` flag, which is invaluable for transferring folders: ``` scp -r /local/directory user@remotehost:/remote/directory ``` Sometimes the SSH service runs on a non-standard port, so the `-P` option lets you specify a different port (note the uppercase P): ``` scp -P 2222 /file user@remotehost:/directory ``` Compression can be enabled during transfer with the `-C` flag, speeding up the process for large files: ``` scp -C /file user@remotehost:/directory ``` Preserving file modification and access times with `-p` ensures the destination files retain their timestamps: ``` scp -p /file user@remotehost:/directory ``` If you want to use a specific SSH private key for authentication, the `-i` option allows specifying the key file: ``` scp -i /path/to/private_key /file user@remotehost:/directory ``` Advanced SCP usage includes transfer rate limiting to avoid saturating bandwidth using `-l`, verbose output for troubleshooting with `-v`, and support for jump hosts to tunnel SCP through intermediate servers using `-J`. For example, copying files through a jump server: ``` scp -J user@jump-server /file user@remotehost:/directory ``` To selectively transfer files based on naming patterns, you can combine `scp` with `find` on the source host, using exec commands: ``` find /path -type f -name "*.log" -exec scp {} user@remotehost:/target/path \; ``` Security-wise, SCP now defaults to the more secure SFTP protocol internally, even though the legacy SCP protocol is deprecated. Using secure ciphers like `aes256-cbc` ensures encrypted and private file transfers. In summary, mastering these SCP commands and options can greatly improve your efficiency and security when working with file transfers on Linux and other UNIX-like systems. For comprehensive visual guides and infographics related to Linux and cybersecurity, consider exploring resources at study-notes.org.