How to Copy a Folder from Remote to Local Using Scp in Linux

Avatar

By squashlabs, Last Updated: October 21, 2023

How to Copy a Folder from Remote to Local Using Scp in Linux

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: How To Find Files Based On Wildcard 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: How to Apply Chmod 777 to a Folder and its Contents in Linux

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:

Tutorial: Using Unzip Command in Linux

This article provides a detailed guide on how to use the unzip command in Linux for file extraction. It covers various topics such as basic usage, listing files in a zip... read more

Tutorial on Linux User Management: How to Create a User

User management is a crucial aspect of the Linux operating system, and this article provides a guide on creating and managing users. From adding users to groups and... read more

Using Linux Commands to Find File and Directory Sizes

Retrieving file and directory sizes in Linux is made simple with the du and ls commands. Learn how to use these commands to find the sizes of files and directories in a... read more

How to Sync Local and Remote Directories with Rsync

Learn how to use Rsync to synchronize local and remote directories in Linux with this step-by-step tutorial. Discover the installation and configuration process, as well... 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 sequences and using tput.... read more

How To Stop A Process Running On A Specific Port In Linux

Guide on terminating a process running on a particular port in Linux. Learn how to stop a process using the lsof and fuser commands. Additionally, find some useful notes... read more