How do I copy a folder from remote to local using scp?
Categories:
Copying Folders from Remote to Local with SCP

Learn how to securely transfer entire directories from a remote server to your local machine using the scp
command-line utility.
The scp
(secure copy) command is a powerful and widely used command-line utility for securely transferring files and directories between hosts on a network. It uses SSH for data transfer and provides the same authentication and security as SSH. This article will guide you through the process of copying a folder from a remote server to your local machine.
Understanding the SCP Command Syntax
The basic syntax for scp
is similar to cp
(copy), but it includes remote host and path information. When copying from a remote location to a local one, you specify the remote source first, followed by the local destination.
graph TD A[Local Machine] --> B{scp command} B --> C[Remote Server] C --> D["Source Folder (remote)"] D --> B B --> E["Destination Folder (local)"] E --> A
Conceptual flow of SCP copying a remote folder to a local machine.
scp -r user@remote_host:/path/to/remote/folder /path/to/local/destination
Basic scp
command for copying a remote folder to local.
Let's break down the components of this command:
1. scp
The command itself, initiating the secure copy process.
2. -r
This crucial option stands for 'recursive'. It tells scp
to copy directories and their contents recursively. Without this, scp
would only attempt to copy a file and would fail on a directory.
3. user@remote_host
This specifies the remote server. user
is the username you use to log into the remote server, and remote_host
is either the IP address or the hostname of the remote server.
4. :/path/to/remote/folder
This is the full path to the folder you want to copy on the remote server. The colon :
separates the remote host information from the remote path.
5. /path/to/local/destination
This is the full path on your local machine where you want the remote folder to be copied. If the destination folder does not exist, scp
will create it. If it exists, the remote folder will be copied inside it.
Practical Examples and Common Scenarios
Here are a few practical examples to illustrate how to use scp
for different scenarios.
~
refers to the home directory of the current user on the respective machine (remote or local). For example, user@remote_host:~/documents
refers to the documents
folder in the user
's home directory on the remote host.# Example 1: Copying a folder from a remote user's home directory to your local current directory
scp -r myuser@192.168.1.100:~/my_project_data .
# Example 2: Copying a folder from a specific remote path to a specific local path
scp -r admin@example.com:/var/www/html/backup /Users/myusername/Desktop/server_backups
# Example 3: Copying a folder using a non-standard SSH port (-P option)
scp -r -P 2222 devuser@devserver.com:/opt/app_logs /var/log/local_app_logs
Practical scp
examples for various remote-to-local folder copy operations.
Authentication and Security Considerations
scp
relies on SSH for authentication. This means you'll typically be prompted for a password for the remote user unless you have SSH key-based authentication set up. Using SSH keys is highly recommended for automation and enhanced security.
scp
will copy the source folder into that directory. If you specify a non-existent directory, scp
will create it and copy the contents there. Always double-check your paths to avoid unintended overwrites or misplaced files.For more advanced usage, such as limiting bandwidth or preserving file attributes, consult the man scp
page on your terminal.