16 Quick SCP Command Examples in Linux

To copy files and folders from one server to another server we may need to use file sharing services such as NFS or samba (SMB) sharing. It is not practical to create an NFS or samba server just to copy files and folders. Linux provides an alternative solution where we can use scp command-line utility tool to securely copy files from one machine to another.

Scp , Also known as secure copy, is used to copy files between hosts over the network. Scp protocol uses SSH for data transfer using the same authentication with the same security as SSH. Just like SSH, you need to know the credentials of the remote machine for authentication. In this article, we will cover 16 quick scp command with examples. Let’s start with syntax of scp command.

Syntax

scp [options] [files or directory] [destination_username]@[target host]:/[destination path]

Where:

  • [files or directory] is the source file or directory you want to copy.
  • [destination username] is the user on destination host
  • [target host] is the hostname or IP of the destination
  • [destination path]  is the path of the destination host where you want to keep the copied files/folder.

Some of the possible options of scp command are :

  •  -C       Enable Compression during transfer
  •  -i         ssh private key
  •  -l         limit the bandwidth during transfer
  •  -P        SSH port number of destination host
  •  -p        Preserves permissions
  •  -q        Suppress warning message and output
  •   -r      Copy files and directory recursively
  •   -v      verbose output

Let’s deep dive into day to day scp command examples

1. Copy file from local server to remote server

To copy a file collection.txt from your current working directory to tmp file of another server use the following command.

$ scp collection.txt root@192.168.178.128:/tmp

2. Copy Multiple files from local server to remote server

To copy multiple files to a remote machine, you can use scp command with file name. In this example, multiple files are copied to a remote machine.

$ scp /tmp/config.yaml /var/log/message.log root@192.168.178.128:/root/Downloads

3. Copy directory instead of files

Sometimes you may need to copy the directory instead of files separately. You can use -r option to copy entire directory to remote machine. It will also ensure that files and sub-directories are copied recursively on destination host.

$ scp -r /var root@192.168.178.128:/root/

4. Find the copy status (verbose output)

By using the option -v along with file name, you can find the copy status or the verbose output of scp command

$ scp -v /var/log/syslog root@10.4.3.201:/root/

Output :

scp-command-verbose-output

5. Preserving source files/directory timestamp

When we copy files/directories to a remote host, the copied files show the latest timestamp on the destination side. To copy files / directories exactly as source, use scp command with option -p so exact timestamp and permissions will be copied.

$ scp -p myfile.txt root@10.4.3.201:/root/collection

Output:

Timestamp-Source-File-scp

Timestamp-Destination-host-scp

In case you are copying a directory and want to maintain source timestamp, then use ‘-rpv’ option.

$ scp -rpv /var/log  root@10.4.3.201:/root/collection

6. Compress files/ directories during the copy or transfer

By using scp command with option -C , files/directories will be compressed during the transfer  to the destination server. Data compression takes place at the network level and in the destination server, data size will be the same as source host.

$ scp -C ubuntu-20.04-iso.tar.gz root@192.168.178.128:/root/Downloads

7. Use ssh key file instead of password

If the ssh configuration is made with private key authentication only then you may need to use a key file to access the remote machine. Files can be copied to destination using scp command along with key file as:

$ scp -i private.key file root@192.168.178.128:/root/Downloads

Where, private.key file is the ssh private key for remote server authentication

8. Suppress scp command output

Scp command generate output such as error notifications, progress meter and warnings, these can be suppressed with option -q as:

$ scp -q -r /var/spool/mail  root@192.168.178.128:/root/Downloads

9. Scp command to limit bandwidth

Using scp command, the bandwidth of file transfer can be limited by using -l option. In the below example, the bandwidth is limited to 100 kbit/s.

$ scp -l 100 ubuntu-20.04.tar.gz root@192.168.178.128:/root/Downloads

10. Transfer file using different ssh port

If you want to use different port than default port 22 to transfer the files and directories, then you can use scp command with option -P and specify the port number as:

$ scp -P 8022 /mnt/config.txt  root@192.168.178.128:/root/Downloads

Where 8022 is the ssh port of the remote host.

11. Use DES encryption to transfer the file

By default, AES-128 encryption algorithm is used by Linux to encrypt files and folders during transfer. Other encryption algorithms can be used by using option -c along with scp command. In this example, 3des-cbc cipher is used for encryption.

$ scp -c 3des-cbc file root@192.168.178.128:/root/Downloads

12. Copy a file from remote host to local server 

Files/Directories can be copied from remote host to local using scp command as:

$ scp root@192.168.178.128:/root/Downloads/file  /tmp

Where , root is the remote server’s username 192.168.178.128 is the IP address of the remote host and file is the name of the file.

To copy directory from remote host to local server use the scp command with -r option as:

$ scp -r root@192.168.178.128:/root/Documents /var/tmp

Where, Documents is the name of directory on remote host.

13. Transfer files across two remote hosts 

You can use scp to copy files between two hosts also. In the following example, pratik’s  file from the home directory is transferred to the umesh tmp folder on a different server. Make sure you have access to both source and destination server.

$ scp pratik@172.10.1.16:~/backup.tar.gz umesh@172.10.1.17:/tmp/

14. Transfer file based in IPv4 address only

If you want to use only IPV4 address to contact remote server to transfer files/directories, then you need to use -4 option as:

$ scp -4 file root@192.168.178.128:/root/Downloads

If you want to use IPV6 address, then use the scp command as:

$ scp -6 file root@192.168.178.128:/root/Downloads

15. Disable strict file checking during remote to local scp

Strict file checking can be disabled during scp from remote to local using -T option with scp command as:

$ scp -T root@192.168.128.178:/root/Downloads/ubuntu.iso /tmp

Where 192.168.178.128 is the remote host

16.  Getting help on scp

To get more details about scp command, its options and usage, please refer its man page.

$ man scp

Conclusion

In this article, we have learned how to use scp command to copy files/directories securely over the network. If you have any other scp command example in your mind, please leave a comment.

Recommended ReadHow to Install and Use Putty (SSH Client) on Linux desktop

Leave a Comment

16 − 4 =