1. Using SCP:
SCP (Secure Copy) is a command-line utility for copying files and directories between two locations. To copy a folder from a remote server to a local machine, use the following command:
scp -r [username@]host:/path/to/remote/folder /path/to/local/folder
2. Using Rsync:
Rsync is a fast and versatile command-line utility for synchronizing files and directories between two locations. To copy a folder from a remote server to a local machine, use the following command:
rsync -avz [username@]host:/path/to/remote/folder /path/to/local/folder
3. Using SFTP:
SFTP (Secure File Transfer Protocol) is a secure file transfer protocol used to transfer files between two remote systems. To copy a folder from a remote server to a local machine, use the following command:
sftp [username@]host:/path/to/remote/folder /path/to/local/folder
Sometimes you need to take a backup of the directory/folder or file from the remote server to the local server. When you have to copy directories/folders or files from the remote server to the local server. For this, in this tutorial, you will learn simple 3 methods using which you can copy remote server directory/folder or files to the local machine in linux.
How to Copy a Folder from Remote to Local in Linux using scp, rsync, SFTP
Using the following methods, you can copy folders/directories from the remote server to the local in linux:
- Method 1: Using SCP (Secure Copy)
- Method 2: Using Rsync
- Method 3: Using SFTP (Secure File Transfer Protocol)
Method 1: Using SCP (Secure Copy)
Firstly, open your terminal and execute the SCP linux command to copy a folder from a remote server to a local machine using SCP:
scp -r [email protected]:/path/to/folder /path/to/local/folder
Here’s a breakdown of the scp
command and its arguments:
scp
: This is the command used to copy files and directories between two systems securely.-r
: This option enables the recursive mode, which copies directories and their contents recursively.username
: This is the username of the user account on the remote system.@
: This symbol separates the username and the hostname of the remote system.remote
: This is the hostname or IP address of the remote system.:/path/to/folder
: This is the absolute path to the folder on the remote system that you want to copy./path/to/local/folder
: This is the absolute path to the local folder where you want to copy the remote folder.
Method 2: Using Rsync
In this method, firstly, connect to the remote server by executing the following command on terminal or command line:
ssh [email protected]
Note that, please replace the following in the above command
- “remoteuser” with the username of the remote server
- “remote.server.com” with the IP address or hostname of the remote server
Once you have connected to the remote server using the above-given command. After that, execute the following command on the terminal to navigate to the folder that you want to copy:
cd remote_folder_path
Note that:- Replace “remote_folder_path” with the path to the folder you want to copy.
Execute the following command on terminal to copy a folder from a remote server to a local machine using Rsync:
rsync -avz -e ssh [email protected]:/path/to/folder /path/to/local/folder
Here’s a breakdown of the rsync command:
rsync
: The command to start the rsync process.-avz
: The flags used to specify the options for the transfer:-a
: The archive flag. This flag is used to preserve the permissions, ownership, timestamps, and other attributes of the files being transferred. It is equivalent to using the options-rlptgoD
.-v
: The verbose flag. This flag is used to show the progress and details of the transfer in the terminal.-z
: The compression flag. This flag is used to compress the data during transfer, which can help to reduce the amount of data being transferred over the network.
-e ssh
: The flag used to specify the remote shell to use for the transfer. In this case, ssh is used as the remote shell to securely transfer data over the network.[email protected]:/path/to/folder
: The source of the transfer. This specifies the username, remote host, and path to the folder on the remote host from which the files will be transferred./path/to/local/folder
: The destination of the transfer. This specifies the path to the local folder where the files will be transferred.
Method 3: Using SFTP (Secure File Transfer Protocol)
In this method, you can also copy or transfer directories or files from remote server to local.
Firstly, open your terminal or command line and execute the following command into it to connect to the remote server using SFTP:
sftp [email protected]
After this, a prompt will open, in which you will have to enter the password to connect to your server.
Once you are connected to the remote server, execute the following command at a terminal or command prompt to navigate to the directories or folders that you want to copy:
cd directory_name
Execute the get following command on terminal or command line to copy the folder to the local machine
get -r foldername /path/to/local/folder
Here’s a breakdown of the get command:
get
: This is the command used in SFTP to retrieve files or directories from the remote server and copy them to the local machine.-r
: This is an option or flag used with theget
command to recursively copy the contents of a directory along with its subdirectories and files.foldername
: This is the name of the folder that you want to copy from the remote server to the local machine./path/to/local/folder
: This is the destination path on the local machine where you want to copy the folder. The folder will be copied to the specified location along with its contents.
Conclusion
That’s it. In this tutorial, you have learned three popular methods: SCP, Rsync, and SFTP, that can copy your folders/directories or files from a remote server to local. Each method has its advantages and disadvantages, so choose the one that best suits your needs.
Recommended Tutorials