Recursive SCP file copy

Overview

SCP is a command line tool which allows files and directories to be copied between two Linux servers or computers via a network – The tool is very similar to the commonly used Linux copy tool CP, however it provides the same functionality via a Secure SSH connection, therefore allowing secure and remote file copy.

As the tool works over vanilla SSH, there are no additional services that you need to install, unlike if you were to use something like FTP – So it’s fairly simple, ubiquitous and lightweight.

Within this guide we cover how to use SCP, and also how to perform Recursive File copy via SCP, this means not just copying a single or list of files, but copying all of the other directories and files that are contained within a specific file path.

TLDR; The Quick Answer

Simply pass the -r argument to the SCP command to perform a recursive file copy, of all files and directories – As shown in the example here:

scp -r /temp/local_path ssh_user@ssh_host:/tmp/remote_path
- or -
scp -r ssh_user@ssh_host:/tmp/remote_path /temp/local_path

SCP Commands / Syntax

The Syntax when using SCP is very familiar to anyone that’s copied files on a command line before, using the standard CP tool on Linux/Unix based systems.

There are typically four parts to a valid SCP command – First we call “SCP” to invoke the tool, followed any additional arguments, then a definition of where the source file path, and finally the destination file path.

  • scp – This always comes first, and is the actual name of the tool
  • OPTIONS – This is any additional arguments that we wish to pass, such as -r
  • SOURCE_PATH – This is where we will copy files/directories from
  • DESTINATION_PATH – This is where we will copy files/directories to
scp [OPTIONS] [SOURCE_PATH] [DESTINATION_PATH]

Paths are interchangeably either Local or Remote, simply the syntax that you use to define the path will instruct the command if this is a remote or local path – This means you can copy away from, or towards, the server your issuing the command from just based on the syntax.

Local paths just follow the normal linux file path syntax, there really isn’t anything specific here.

/var/log

Remote paths do require some additional information, but follow a standard URI format – You need to give the SSH username, the SSH Hostname or IP address, and finally the remote path to copy from or to.

[ssh_user]@[ssh_hostname]:/[remote_file_path]

We bring all this together in the below example – Here we copy all files and folders within “/var/log” on the local machine that we run the command on, over to the remote host 192.168.1.120, within the folder “/tmp/log_backup” and authenticated as the user “root”, we also add the -r argument to make the copy recursive.

scp -r /var/log root@192.168.1.120:/tmp/log_backup/

1 – SCP Copy Single File

Single files are very simple to copy, you don’t need to provide any additional arguments to the SCP command – Simply provide a path to the source file, and then a path to the destination file.

This would be an example of copying a file from a remote server back to our local server/workstation.

scp user@hostname:/source-file /destination-file

Below is the same command but in reverse, so we are copying a local file to a remove server via SSH.

scp /source-file user@hostname:/destination-file

In both examples once you enter the command you will be asked for a password, this will be the password of the SSH user, unless you have SSH keys for transport then the file copy will happen straight away, or will be a prompt for the key encryption password.

2 – SCP Multiple File Copy

Just like the CP command the file paths work with wildcards * when specifying a file name or directory,  this means instead of copying one file at a time, we could copy all the files in a directory, here’s an example:

scp  /source-folder/* user@hostname:/destination-folder/

The above will copy everything immediately in in that folder, however we can be a little more clever and say just copy all the files with a *.txt extension, for example:

scp  /source-folder/*.txt user@hostname:/destination-folder/

However it’s worth noting that like the Linux copy (cp) command by default the SCP command will only copy files, and not directories or their contents – Next we will see how these can also be copied, with a secure recursive file copy.

3 – SCP Recursive File Copy

As we saw above we can easily copy multiple files at once, however sometimes we also want to copy directories and their contents too, for that we’ll use the recursive copy argument when calling the SCP command – Again this will work in any direction, the remote end can either be the source or destination.

scp -r root@hostname:/source_folder/* /destination_folder/

The command will also work if the direction of SSH was switched:

scp -r /source_folder/* root@hostname:/destination_folder/

4 – Preserving Modification Properties and Access Times

Files and Directories contain a number of different meta data on Linux, and the default behaviour of SCP will not copy this information from one host to another, however with the -p argument passed we can instruct SCP that we wish to retain this information.

This will instruct SCP to preserve the modification times, access times and modes from the source files on the command.

scp -r -p root@hostname:/source_folder/* /destination_folder/

The command will also work if the direction of SSH was switched:

scp -r -p /source_folder/* root@hostname:/destination_folder/

General Notes

Make sure you keep copies of files, especially when performing a recursive copy as it will not warn you if your about to overwrite files at the destination.

SSH keys are very useful if you are constantly coping files as it can save you from having to enter a password each time, and they are also more secure than traditional passwords, however be sure to keep them secure!

Technology enthusiastic with many ongoing online projects one of which is this personal blog PingBin. While also working full time within a data center designing and maintaining the network infrastructure.

Leave a reply:

Your email address will not be published.