How to copy directory with SCP

The Unix command scp (which stands for “secure copy protocol”) is a simple tool for uploading or downloading files (or directories) to/from a remote machine. The transfer is done on top of SSH, which is how it maintains its familiar options (like for specifying identities and credentials) and ensures a secure connection. It’s really helpful to be able to move around files between any machine that supports SSH.

When trying to copy directory like you usually copy a file with scp you may encounter the following scp error:
folder_name: not a regular file

Downloading a Directory with SCP

In this use-case, we want to use scp to download a directory from a remote server to our local machine. To achieve this, we’ll use the -r flag, which tells scp to recursively copy all of the directory’s contents to our machine.

Here is an example of using scp to download a directory:

Pretty simple, right? The -r flag is the only difference between downloading a single file and downloading an entire directory. With -r specified, the directory tree is recursively traversed and each file encountered is downloaded.

One important thing to note is that scp does follow symbolic links within directories, so just be aware in case this matters for your purposes.

Uploading a Directory with SCP

The same exact concepts as downloading a directory apply here as well. You’ll probably notice that the only difference is where we specify the source directory within the actual command.

Here is an example of using scp to upload a folder:

When the source path comes first, like in the example above, it is assumed to be referring to a directory on your local machine, which is then recursively transferred to the destination machine thanks to the -r flag, as before.

Good luck!

Want me to do this for you? Drop me a line: itgalaxyzzz {at} gmail [dot] com