How do I copy a folder from remote to local using scp?
Categories:
How to Copy a Folder from Remote to Local using SCP

Learn the essential scp command for securely copying entire directories from a remote server to your local machine, including common options and practical examples.
The scp (secure copy) command is a powerful tool for transferring files and directories between hosts on a network. It uses SSH for data transfer, providing the same authentication and security as SSH. This article will guide you through the process of copying an entire folder from a remote server to your local machine using scp.
Understanding the scp Command Syntax
The basic syntax for scp involves specifying the source and destination paths. When copying from a remote host, you'll need to include the remote user and host details in the source path. The -r option is crucial for recursive copying of directories.
scp -r user@remote_host:/path/to/remote/folder /path/to/local/destination
The basic structure of an scp command for copying a remote folder to a local directory.
-r flag stands for 'recursive' and is absolutely necessary when copying directories. Without it, scp will only attempt to copy files and will fail on folders.Practical Examples and Common Scenarios
Let's look at some practical examples to illustrate how to use scp effectively. These examples cover common use cases and demonstrate how to handle different remote paths and local destinations.
scp -r myuser@example.com:~/my_remote_folder .
This command copies my_remote_folder from the user's home directory on example.com to the current directory on your local machine. The . signifies the current directory.
scp -r admin@192.168.1.100:/var/log/nginx/ /home/localuser/server_logs
Copies the nginx log folder from the remote server at 192.168.1.100 to /home/localuser/server_logs on your local machine.
scp command (user@remote_host) has read permissions for the remote folder you are trying to copy. Otherwise, the transfer will fail.Advanced Options and Best Practices
While the basic command is straightforward, scp offers several options to enhance your transfer experience, such as specifying a different port, limiting bandwidth, or preserving file attributes. Always consider these for more robust operations.
scp -r -P 2222 user@remote_host:/path/to/remote/folder /path/to/local/destination
Use the -P option (uppercase) to specify a non-standard SSH port.
scp -r -l 800 user@remote_host:/path/to/remote/folder /path/to/local/destination
The -l option limits the bandwidth in Kbit/s. Here, it's set to 800 Kbit/s (100 KB/s).
rsync instead of scp. rsync is more efficient as it can resume interrupted transfers and only copies changed parts of files.
Process flow for SCP remote to local folder copy.