SCP is a means of transferring files from one computer or server to another, via a network connection, it’s basically like having the ability to perform the linux CP command across a network but securely, as the protocol uses SSH for transport.
It’s a very useful useful tool that many people use to push or pull data between two or more devices, reason why I mentioned push or pull is if you have a server that doesn’t have an SSH Service installed, you can still pull the content so long as the other server has an SSH service installed, even if it’s something with a light footprint such as Dropbear.
Commands / Syntax
The Syntax when using SCP is very familiar to anyone that’s copied files on a command line before, namely using the standard CP tool on Linux/Unix based systems.
There are typically three parts to a command, “SCP” to invoke the tool, followed by a definition of where the source files are, and then where the destination files are to go. Typically one side will be local, the other will be remote, however these can interchange.
1) Simple Single File Copy
In the below example we are creating an SSH connection to the source server for secure transport, and pulling the file named “source-file” to a local file called “destination-file” on the server that we run the command.
scp user@hostname:/source-file /destination-file
Here is a similar command, but with the roles switched, we’re getting a local file and creating an SSH connection to a remote server, then pushing this local file to a remote destination.
scp /source-file user@hostname:/destination-file
In both examples one 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.
2) Multiple File Copy
Just like the CP command we can use wildcards * when specifying a file name, 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/
3) Multiple Recursive File Copy
There’s a problem with the above, they will copy files and not directories, similarly they will not copy files within the child directory even when you use the wildcard *.
To overcome that we use the recursive flag -r in the same command layout as above, for example in the below if we have a folder called “images” within the source folder, this and all of it’s contents will be copied over to the destination filter.
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/
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, however be sure to keep them secure!