How to SCP to a Folder in Linux: Copy Folder Remote<>Local

Avatar

By squashlabs, Last Updated: Aug. 6, 2024

How to SCP to a Folder in Linux: Copy Folder Remote<>Local

To copy a folder from a remote server to your local machine using the scp command in Linux, you can follow these steps:

Step 1: Establish a Secure Connection

Make sure you have a working SSH connection to the remote server. If you don't, you can use the following command to establish a secure connection:

ssh username@remote_server_ip

Replace username with your actual username and remote_server_ip with the IP address or hostname of the remote server.

Related Article: Using a Watchdog Process to Trigger Bash Scripts in Linux

Step 2: Navigate to the Desired Local Directory

Open a terminal on your local machine and navigate to the directory where you want to copy the folder. You can use the cd command to change directories. For example, to navigate to the Documents directory:

cd ~/Documents

Step 3: Copy the Folder Using Scp

Once you are in the desired local directory, you can use the scp command to copy the folder from the remote server. The basic syntax for copying a folder is as follows:

scp -r username@remote_server_ip:/path/to/folder .

Replace username with your actual username, remote_server_ip with the IP address or hostname of the remote server, and /path/to/folder with the actual path to the folder on the remote server.

The -r flag is used to copy directories recursively. It ensures that all files and subdirectories within the folder are also copied.

The . at the end of the command specifies the current directory as the destination. If you want to specify a different destination directory, you can replace . with the desired path.

Example:

Let's say you have a remote server with the IP address 192.168.0.100, and you want to copy the folder /home/username/data from the remote server to your local machine's Documents directory.

Assuming your username is john, you can use the following command:

scp -r john@192.168.0.100:/home/username/data ~/Documents

This command will copy the data folder from the remote server to the Documents directory on your local machine.

Related Article: Fixing the 'Linux Username Not In The Sudoers File' Error

Alternative Method: Using rsync

Another way to copy a folder from a remote server to your local machine is by using the rsync command. Rsync is a useful utility that can synchronize files and directories between different locations.

To copy a folder using rsync, you can use the following command:

rsync -avz username@remote_server_ip:/path/to/folder .

This command is similar to the scp command, but it has some additional options:

- -a preserves the directory structure and file attributes.

- -v enables verbose output, showing the progress of the transfer.

- -z compresses the data during the transfer, reducing the network usage.

The . at the end of the command specifies the current directory as the destination. You can replace . with the desired path to specify a different destination directory.

Example:

Let's use the previous example to copy the /home/username/data folder from the remote server to your local machine's Documents directory using rsync.

Assuming your username is john and the remote server's IP address is 192.168.0.100, you can use the following command:

rsync -avz john@192.168.0.100:/home/username/data ~/Documents

This command will copy the data folder from the remote server to the Documents directory on your local machine.

Best Practices

- Before copying a large folder, consider compressing it into a single archive file on the remote server using the tar command. This can significantly reduce the transfer time and bandwidth usage.

- If you need to copy files frequently between the same remote server and local machine, you can set up SSH key-based authentication to avoid entering the password every time. This can be done by generating an SSH key pair and adding the public key to the remote server's authorized_keys file.

- Always double-check the source and destination paths to ensure that you are copying the correct folder and to the intended location.

- If the remote server is behind a firewall or uses a non-standard SSH port, you may need to specify additional options in the scp or rsync commands. Refer to their respective documentation for more information.

- For security reasons, ensure that your remote server is properly configured and uses strong SSH encryption protocols.

More Articles from the The Linux Guide: From Basics to Advanced Concepts series:

Displaying Images using Wget Bash Script in Linux

Learn how to display images in Linux using a wget bash script. This article covers everything from downloading an image with wget to displaying it in… read more

How to Fix 'Undefined Reference to pthread_create' in Linux

A guide to address the 'Undefined Reference to pthread_create' issue in Linux. This article covers checking for the pthread library installation, add… read more

Exploring Local Scope of Bash Variables in Linux Scripts

Decoding the locality of bash variables in Linux scripting. This article explores the concept of local variables in bash scripting, their declaration… read more

Evaluating the Benefits of Bash Scripting in Linux

Bash scripting in Linux offers a range of benefits, making it an essential tool for developers and system administrators. From automating repetitive … read more

How to Continue a Bash Script Post cURL Command

When working with bash scripts in Linux, it's important to know how to continue your script after executing a curl command. This article will guide y… read more

How to Alter the Echo Output Colors in Linux

This article provides a simple guide on modifying the output color of echo in Linux using bash color. It covers two methods: using ANSI escape sequen… read more

Adding Color to Bash Scripts in Linux

Adding color to Bash scripts in Linux can greatly enhance the readability of your output. This article will guide you in understanding color codes fo… read more

Accessing Seconds Since Epoch in Bash Scripts

Detailed instructions on how to access seconds since epoch in bash scripts in a Linux environment. Learn how to convert epoch time to a readable date… read more

How to Format Numbers to Two Decimal Places in Bash

Learn how to format numbers to two decimal places in bash scripting on Linux. This article covers rounding, floating point arithmetic, and various me… read more

Tutorial on Traceroute in Linux

Traceroute is a powerful command in Linux systems that allows you to trace the path of network packets from your computer to a destination. This tuto… read more