Linux copy directory and contents from remote to local & vice versa

In this tutorial I will share commands and examples to cover below scenarios:

  • Unix and Linux copy directory and contents using rsync remote to local server
  • Unix and Linux copy directory and contents using rsync to remote server
  • Unix and Linux copy directory and files using scp from local to remote server
  • Unix and Linux copy directory and files using scp from remote to local server
  • scp recursive to copy directory and contents in Unix and Linux
  • Unix and Linux copy file from ssh to local server

 

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.

 

Copy directory and files from local to remote server

Linux copy directory and contents from remote to local & vice versa

 

1. Linux copy directory and files with scp recursive

  • scp is a secure remote copy tool which is used to copy directory and contents between multiple Linux server.
  • To copy only files from local to remote server, you do not need any extra argument with scp.
  • But to copy directory and contents we need scp recursive using "-r" argument
  • If you use scp without '-r' then the tool can only copy files (and not directories) from local to remote server or vice versa.

 

1.1: Keep "same" directory name with scp from local to remote server

In this scp syntax, we will keep the same directory name after copying directory and its contents to remote server

scp syntax:

scp -r <path to dir on localhost> user@<host>:<path on remote host>/

Let me copy all files in directory /tmp/deepak from local to remote server under /home/temp/ on the remote server.

Below are the /tmp/deepak directory content on my localhost, so I will copy all files in directory /tmp/deepak with scp recursive to remote server.

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 "/" after giving the directory name on localhost with scp (as highlighted).

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 server

If you wish to copy directory /tmp/deepak to remote server using a different directory name then use the below syntax

scp syntax:

scp -r <path to dir on localhost>/* user@<host>:<path on remote host>/

Here if you observe the scp syntax, I have provided "/*" at the end of directory name in localhost. So here we copy all files in directory /tmp/deepak and store it under /home/temp/rahul/ on remote server

So you see this forward slash (/) is very important for scp from local to remote server.

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 /tmp/deepak are successfully copied to remote server. Validate the content on server2 node.

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

  • rsync is another better alternative to copy directory and contents from local to remote server in Linux and Unix.
  • It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination.
  • Rsync is widely used for backups and mirroring and as an improved copy command for everyday use
  • Rsync finds files that need to be transferred using a "quick check" algorithm (by default) that looks for files that have changed in size or in last-modified time.

 

2.1: Keep "same" directory name with rsync from local to remote server

In 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 <path to dir on localhost> user@<host>:<path on remote host>/

In this example we will use rsync to copy directory and contents from (/tmp/deepak) to remote host under /home/temp

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 server

Next we will use rsync to copy directory and contents of /tmp/deepak to a different folder on the remote server inside /home/temp/rahul.

So we will change the directory name from deepak on localhost to rahul on remote server

rsync syntax:

rsync -avz <path to dir on localhost>/* user@<host>:<path on remote host>/

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 server

We 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

Linux copy directory and contents from remote to local & vice versa

 

1. Linux copy directory and files with scp recursive from remote to local server

We 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 -r argument.

 

1.1: Keep "same" directory name with scp from remote to local server

Check the scp syntax to copy files from remote to local server for more details

Syntax for scp:

scp -r <user>@<host>:<source path on remote host> <destination on localhost>/

To scp from remote to local server, below is the content on my remote host (server2) under /home/temp/deepak which I wish to copy on my localhost (server1) under /tmp/deepak

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 scp from remote to local server in Linux and Unix

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 server1 under /tmp/ where we had copied the content. As you see we have directory deepak now on our localhost (server1)

[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 server

To copy directory and contents from remote to local server with different directory name then you must use forward slash carefully
We must also provide a local directory in the source path on (server1) under which you want to copy files and folders from remote server (server2)

Syntax for scp:

scp -r <user>@<host>:<source path on remote host>/* <destination on localhost>/

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 server1 under /tmp/rahul. So the content of directory deepak from server2 is successfully copied under rahul 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 server

We 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. server1 for our environment

 

2.1: Keep "same" directory name with rsync from remote to local server

Notice 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 rsync command to copy directory from remote to local server

Syntax for rsync:

rsync -avz <user>@<host>:<source path on remote host> <destination on localhost>/

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 /tmp/deepak on server1

[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 server

Now to change the directory name or store the directory contents to a different folder from remote to local server (server1) we must use forward slash in the source path from remote server (server2)

Check the rsync syntax, as you see I have defined a forward slash in the source path from (server2)

Syntax for rsync:

rsync -avz <user>@<host>:<source path on remote host>/* <destination on localhost>/

In this rsync example I will copy all directory files from /home/temp/deepak/ on (server2) to /tmp/rahul on localhost (server1)

[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 server1 under /tmp/rahul

[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.

 

4 thoughts on “Linux copy directory and contents from remote to local & vice versa”

  1. Hello,
    I think there is a mistake in the content you provided. I noticed that there is wrong command in copying folder from remote to local system using rsync command. You can go through that and let me know if I'm correct.

    Reply

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.