Linux Commands

SCP Multiple Files

SCP is a powerful command-line utility that allows you to copy files and directories between remote machines. SCP uses the SSH protocol to securely transfer files between the connected hosts.

As a tool, SSH requires you to provide a password for the SSH user or an SSH key for the target host. In this tutorial, we will explore how to copy files and directories using SCP command.

SCP Command

The SCP command is available in any system that has the ssh-client and server packages installed. You can then invoke the SCP command with the following syntax:

$ scp user@source_host:source_filename user@destination_host:destination_filename

The previous command takes few parameters such as:

  1. user@source_host – refers to the username and the address of the source machine. You also need to specify the source filename after the colon as shown in the previous syntax.
  2. user@destination_host – specifies the username and address of the target host to which you wish to transfer the files.

Ensure that the specified user has the read and write permissions on the target file or directory of the remote host.

You can also specify other options in the SCP command to alter its default behavior. Accepted options include:

  1. -P – sets the port for the remote host
  2. -c – allows SCP to compress the data during transfer
  3. -r – allows SCP to copy files and directories recursively
  4. -q – quiet mode, hides the progress meter and non-error messages
  5. -p – allows SCP to preserve files and directory attributes
  6. -v – Enables verbose mode. Show all descriptive messages and progress state
  7. -l – Limits the bandwidth used by the SCP utility

You can check the other options by running the following command:

$ man scp

Note: Although SCP shows the descriptive messages of the files and directories you are copying, it will not prompt you when overwriting the files. Hence, ensure to provide unique names for the files you copy to avoid data loss.

Copy File from Local to Remote Host

You can copy a file from your local machine to a remote host by running the following command:

$ scp source_file.ext user@remote_addr:/remote/dir

For example, to copy the file hello.txt to /home/debian/files directory, we can run the following command:

$ scp hello.txt debian@172.76.32.113:/home/debian/files

The command will prompt you for the password of the specified remote user. If you have added your SSH key to the remote host, the command will automatically login and transfer the target files.

Keep in mind that the SCP command will preserve the original filename unless specified. For example, to save the file under a different directory, you can run this following command:

$ scp hello.txt debian@172.76.32.113:/home/debian/files/hello_new.txt

This will change the filename from hello.txt to hello_new.txt on the remote machine.

Copy Directory from Local to Remote Host

If you wish to copy a directory from your local machine to a remote host, use the recursive option as shown in the following syntax:

$ scp -r src_dir user@remote_host:/path/on/remote/host/

For example, to copy all the files in the /var/logs directory of the local machine, you can run the following command:

$ scp -r /var/logs/ debian@172.76.32.113:/home/debian/logs

The -r flags tells the SCP command to copy the directory recursively. If the target directory does not exist on the remote host, the SCP will create it automatically.

Copy Files from Host to Remote Host

In some cases, you may need to copy files from a remote host to another remote host. You can accomplish that by running the following command:

$ scp user_1@host_1:src_file user_2@host_2:target_path

The command will copy the files specified in the src_file parameter to the path specified in the target_path parameter.

Ensure that both machines are online and that they can communicate via SSH.

Conclusion

This tutorial walks you through using the SCP command to copy the files and directories via SSH.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list