How copy files from local to remote server?
Show
In this tutorial I will share commands and examples to cover below scenarios:
There are various commands available in Linux to copy directory and contents from one server to another in Linux. I have also written another article on similar topic with 5 commands to copy files from one server to another in Linux In this article I will share the commands and arguments in Linux copy directory and files using scp from local to remote server, scp from remote to local server, rsync remote to local and rsync to remote server in Linux.
1. Linux copy directory and files with scp recursive
1.1: Keep “same” directory name with scp from local to remote serverIn this scp syntax, we will keep the same directory name after copying directory and its contents to remote server scp syntax: scp -r Let me copy all files in directory Below are the server1:~ # ls -l /tmp/deepak/ total 8 -rw-r----- 1 root root 0 May 10 16:38 file1 -rw-r----- 1 root root 0 May 10 16:38 file2 -rw-r----- 1 root root 0 May 10 16:38 file3 drwxr-x--- 2 root root 4096 May 10 16:39 test1 drwxr-x--- 2 root root 4096 May 10 16:39 test2 So next using scp recursive I will copy directory and contents from local to remote Linux server In this example, the directory name will be same on local and remote server as we are not giving " server1:~ # scp -r /tmp/deepak temp@server2:/home/temp/
Password:
file1 100% 0 0.0KB/s 00:00
file3 100% 0 0.0KB/s 00:00
file2 100% 0 0.0KB/s 00:00 The ssh copy file from local to remote was successful. Next validate the transfer on the remote server server2:~ # ll /home/temp/deepak/ total 8 drwxr-x--- 2 temp users 4096 May 10 16:50 test1 -rw-r----- 1 temp users 0 May 10 16:50 file1 drwxr-x--- 2 temp users 4096 May 10 16:50 test2 -rw-r----- 1 temp users 0 May 10 16:50 file3 -rw-r----- 1 temp users 0 May 10 16:50 file2 1.2: Change directory name with scp from local to remote serverIf you wish to copy directory scp syntax: scp -r Here if you observe the So you see this forward slash (/) is very important for Execute the command in below format server1:~ # scp -r /tmp/deepak/* temp@server2:/home/temp/rahul/ Password: file1 100% 0 0.0KB/s 00:00 file2 100% 0 0.0KB/s 00:00 file3 100% 0 0.0KB/s 00:00 All files under directory server2:~ # ls -l /home/temp/rahul/ total 8 -rw-r----- 1 temp users 0 May 10 16:54 file1 -rw-r----- 1 temp users 0 May 10 16:54 file2 -rw-r----- 1 temp users 0 May 10 16:54 file3 drwxr-x--- 2 temp users 4096 May 10 16:54 test1 drwxr-x--- 2 temp users 4096 May 10 16:54 test2 2. Copy folder and files using rsync from local to remote server
2.1: Keep “same” directory name with rsync from local to remote serverIn this rsync syntax, we will change the directory name after copying directory and its contents to remote server. The logic remains the same for both scp and rsync rsync syntax: rsync -avz In this example we will use rsync to copy directory and contents from ( server1:~ # rsync -avz /tmp/deepak temp@server2:/home/temp/ Password: sending incremental file list deepak/ deepak/file1 deepak/file2 deepak/file3 deepak/test1/ deepak/test2/ deepak/test2/ sent 240 bytes received 81 bytes 49.38 bytes/sec total size is 0 speedup is 0.00 Follow rsync man page for more details. Here, -a | --archive This is equivalent to -rlptgoD. It is a quick way of saying you want recursion and want to preserve almost everything (with -H being a notable omission). -r | --recursive This tells rsync to copy directories recursively -l | --links When symlinks are encountered, recreate the symlink on the destination. -p | --perms This option causes the receiving rsync to set the destination permissions to be the same as the source per‐ missions. -t | --times This tells rsync to transfer modification times along with the files and update them on the remote system. -g | --group This option causes rsync to set the group of the destination file to be the same as the source file. -o | --owner This option causes rsync to set the owner of the destination file to be the same as the source file, but only if the receiving rsync is being run as the super-user -D The -D option is equivalent to --devices --specials. -v | --verbose This option increases the amount of information you are given during the transfer. -z, --compress With this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted -- something that is useful over a slow connection. After rsync copy, validate the transfer on the remote server. So the directory name is same on local and remote server after transfer. server2:/home/temp # ll /home/temp/deepak/ total 8 -rw-r----- 1 temp users 0 May 10 16:38 file3 -rw-r----- 1 temp users 0 May 10 16:38 file2 -rw-r----- 1 temp users 0 May 10 16:38 file1 drwxr-x--- 2 temp users 4096 May 10 16:39 test2 drwxr-x--- 2 temp users 4096 May 10 16:39 test1 2.2: Change directory name with rsync from local to remote serverNext we will use rsync to copy directory and contents of So we will change the directory name from deepak on localhost to rahul on remote server rsync syntax: rsync -avz Next execute the command in the below syntax server1:~ # rsync -avz /tmp/deepak/* temp@server2:/home/temp/rahul/ Password: sending incremental file list file1 file2 file3 test1/ test2/ sent 195 bytes received 77 bytes 108.80 bytes/sec total size is 0 speedup is 0.00 After the transfer, validate the content on the remote node server2:/home/temp # ls -l /home/temp/rahul/
total 8
-rw-r----- 1 temp users 0 May 10 16:38 file1
-rw-r----- 1 temp users 0 May 10 16:38 file2
-rw-r----- 1 temp users 0 May 10 16:38 file3
drwxr-x--- 2 temp users 4096 May 10 16:39 test1
drwxr-x--- 2 temp users 4096 May 10 16:39 test2 Copy directory and files from remote to local serverWe can use the same tool scp recursive and rsync to copy directory and contents from remote to local server in Linux and Unix. Although the syntax to copy from ssh to local will vary for rsync and scp 1. Linux copy directory and files with scp recursive from remote to local serverWe will again use scp recursive to perform scp from remote to local server in Linux and Unix To use scp recursive we must use scp with 1.1: Keep “same” directory name with scp from remote to local serverCheck the scp syntax to copy files from remote to local server for more details Syntax for scp: scp -r To root@server2 ~]# ls -l /home/temp/deepak/ total 8 drwxr-xr-x 2 root root 4096 May 9 12:28 dir1 drwxr-xr-x 2 root root 4096 May 9 12:28 dir2 -rw-r--r-- 1 root root 0 May 9 12:28 file1 -rw-r--r-- 1 root root 0 May 9 12:28 file2 -rw-r--r-- 1 root root 0 May 9 12:28 file3 Below is the command to copy directory and contents using NOTE that I have not provided a forward slash (/) after the source directory to keep the same directory name after copy from remote to local server. [root@server1 ~]# scp -r root@server2:/home/temp/deepak /tmp/ Password: file2 100% 0 0.0KB/s 00:00 file3 100% 0 0.0KB/s 00:00 file1 100% 0 0.0KB/s 00:00 Validate the content on localhost [root@server2 ~]# ls -l /home/temp/deepak/ total 8 drwxr-xr-x 2 root root 4096 May 9 12:28 dir1 drwxr-xr-x 2 root root 4096 May 9 12:28 dir2 -rw-r--r-- 1 root root 0 May 9 12:28 file1 -rw-r--r-- 1 root root 0 May 9 12:28 file2 -rw-r--r-- 1 root root 0 May 9 12:28 file3 1.2: Change directory name with scp from remote to local serverTo copy directory and contents from remote to local server with different directory name then you must use forward slash carefully Syntax for scp: scp -r We will use this syntax in our next scp example: [root@server1 ~]# scp -r root@server2:/home/temp/deepak/* /tmp/rahul/ file1 100% 0 0.0KB/s 00:00 file2 100% 0 0.0KB/s 00:00 file3 100% 0 0.0KB/s 00:00 Next verify the content on localhost [root@server1 ~]# ls -l /tmp/rahul/ total 8 drwxr-xr-x 2 root root 4096 May 9 12:43 dir1 drwxr-xr-x 2 root root 4096 May 9 12:43 dir2 -rw-r--r-- 1 root root 0 May 9 12:43 file1 -rw-r--r-- 1 root root 0 May 9 12:43 file2 -rw-r--r-- 1 root root 0 May 9 12:43 file3 2. Copy folder and files using rsync from remote to local serverWe can also use rsync to copy directories and contents from remote to local server using the same arguments but different syntax To copy files and folders from remote to local, you must execute rsync on localhost i.e. 2.1: Keep “same” directory name with rsync from remote to local serverNotice the rsync syntax carefully, we have not used forward slash (/) in the source path, so the entire directory and contents will be copied Similarly you can use below Syntax for rsync: rsync -avz Using below command you can folder from remote to local server [root@server1 ~]# rsync -avz root@server2:/home/temp/deepak /tmp/ receiving incremental file list deepak/ deepak/file1 deepak/file2 deepak/file3 deepak/dir1/ deepak/dir2/ sent 97 bytes received 284 bytes 762.00 bytes/sec total size is 0 speedup is 0.00 Verify the content of [root@server1 ~]# ls -l /tmp/deepak/ total 8 drwxr-xr-x 2 root root 4096 May 9 12:28 dir1 drwxr-xr-x 2 root root 4096 May 9 12:28 dir2 -rw-r--r-- 1 root root 0 May 9 12:28 file1 -rw-r--r-- 1 root root 0 May 9 12:28 file2 -rw-r--r-- 1 root root 0 May 9 12:28 file3 2.2: Change directory name with rsync from remote to local serverNow to change the directory name or store the directory contents to a different folder from remote to local server ( Check the rsync syntax, as you see I have defined a forward slash in the source path from ( Syntax for rsync: rsync -avz In this rsync example I will copy all directory files from [root@server1 ~]# rsync -avz root@server2:/home/temp/deepak/* /tmp/rahul/ receiving incremental file list file1 file2 file3 dir1/ dir2/ sent 93 bytes received 238 bytes 662.00 bytes/sec total size is 0 speedup is 0.00 Validate the content on [root@server1 ~]# ls -l /tmp/rahul/ total 8 drwxr-xr-x 2 root root 4096 May 9 12:28 dir1 drwxr-xr-x 2 root root 4096 May 9 12:28 dir2 -rw-r--r-- 1 root root 0 May 9 12:28 file1 -rw-r--r-- 1 root root 0 May 9 12:28 file2 -rw-r--r-- 1 root root 0 May 9 12:28 file3 So the transfer was successful, I hope I was able to explain the importance of forward slash while copying all files in directories for proper naming. Lastly I hope the steps from the article to perform scp from local to remote server and scp from remote to local server with examples on Linux was helpful. So, let me know your suggestions and feedback using the comment section. How copy folder from local to RDP?2.In general, copy paste files between local and remote PC can be done by following steps.. Right click the file you want to transfer.. Choose Copy from the menu.. Right click the folder (or desktop) you want to transfer to.. Choose Paste.. How copy file from local to SSH server?To copy the files you will need to first invoke the SCP, followed by the remote username@IP address, path to file. If you do not specify the path, it is assumed as default in this case which will be the user's home directory, this will be followed the path where the file will be stored locally.
How copy file from local to server in terminal?To copy files from a local system to a remote server or remote server to a local system, we can use the command 'scp' . 'scp' stands for 'secure copy' and it is a command used for copying files through the terminal. We can use 'scp' in Linux, Windows, and Mac.
|