ssh copy folder from local to remote. ssh copy folder from remote to local server. ssh copy folder from one server to another server. scp copy directory from local to remote. ssh copy directory from remote to local. scp copy directory from one server to another in linux.

2 commands to copy folder from local to remote or vice versa in Linux

There are various methods to copy files and folder from one server to another in Linux. I have already written an article with 5 commands to copy files from one server to another in Linux

In this article I will share the commands and arguments to copy folder from local to remote server in Linux.

 

copy folder local to remote server

You can use either ssh or rsync to copy folder from local to remote server in the same or different directory. If you wish to perform the transfer parallel then you must use pscp or pssh because by default using scp or rsync the transfer will happen sequentially. If you have configured a passwordless key based login between your server then ssh/scp will not prompt you for any password and will transfer the files from one server to another server seamlessly. This is very useful for automation.

.

Using ssh copy folder

scp is a secure remote copy tool which is used to copy files and folder from local to remote or from remote to local server in Linux.

scp copy folder from local to remote to copy a folder from local to remote server in linux we can use scp command with “-r” argument. If you use scp without ‘-r‘ then the tool can only copy files from local to remote server or vice versa.

Syntax:

scp -r <path to dir on localhost> [email protected]<host>:<path on remote host>/

Let me copy entire directory /tmp/deepak from my localhost to remote server under /home/temp/. Below are my content under /tmp/deepak so I will copy all the files and directories recursively under /tmp/deepak 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

Using the below command I will copy folder from local to remote server

server1:~ # scp -r /tmp/deepak [email protected]:/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

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

If you wish to copy the entire content of /tmp/deepak to remote server in a different directory name then use the below syntax

Syntax:

scp -r <path to dir on localhost>/* [email protected]<host>:<path on remote host>/

Execute the command in below format

server1:~ # scp -r /tmp/deepak/* [email protected]:/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

Our files and folder are successfully copied to remote server. Validate the content on client 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

 

Using rsync copy folder

rsync is another better alternative to scp for copying files and folders from local to remote server.

Syntax:

rsync -avz <path to dir on localhost> [email protected]<host>:<path on remote host>/

Similar to scp we will use rsync to perform the transfer of the folder. To copy the entire directory (deepak) from /tmp to remote host under /home/temp

server1:~ # rsync -avz /tmp/deepak [email protected]:/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

Validate on the remote server

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

Next we will copy the content of /tmp/deepak to a different folder on the remote server inside /home/temp/rahul

Syntax:

rsync -avz <path to dir on localhost>/* [email protected]<host>:<path on remote host>/

Next execute the command in the below syntax

server1:~ # rsync -avz /tmp/deepak/* [email protected]:/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

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 folder from remote to local server

Next I will show you the command and syntax to copy folder from remote to local server. To copy folder from remote to local server also we can use the same commands i.e. scp and rsync. The syntax to copy folder from remote to local will only vary

 

Copy folder from remote to local server using scp

Syntax for scp:

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

Below is the content on my remote host under /home/temp/deepak which I wish to copy on my localhost under /tmp/deepak

server2:~ # ls -l /home/temp/deepak/
total 8
-rwxrwxrwx 1 root root    0 May 11 00:21 file1
-rwxrwxrwx 1 root root    0 May 11 00:21 file2
-rwxrwxrwx 1 root root    0 May 11 00:21 file3
drwxrwxrwx 2 root root 4096 May 11 00:21 test1
drwxrwxrwx 2 root root 4096 May 11 00:21 test2

Below is the command to perform the copy folder from remote to local

server1:~ # scp -r [email protected]:/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.

server1:~ # ls -l /tmp/deepak/
total 8
-rwxr-x--- 1 root root    0 May 11 00:22 file1
-rwxr-x--- 1 root root    0 May 11 00:22 file2
-rwxr-x--- 1 root root    0 May 11 00:22 file3
drwxrwxrwx 2 root root 4096 May 11 00:22 test1
drwxrwxrwx 2 root root 4096 May 11 00:22 test2

 

Copy folder from remote to local server using rsync

Similarly you can use below rsync command to copy folder from remote to local server

Syntax for rsync:

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

Using below command you can folder from remote to local server

server1:~ # rsync -avz [email protected]:/home/temp/deepak /tmp/
Password:
receiving incremental file list
deepak/
deepak/file1
deepak/file2
deepak/file3
deepak/test1/
deepak/test2/

sent 80 bytes  received 261 bytes  136.40 bytes/sec
total size is 0  speedup is 0.00

 

Lastly I hope the steps from the article to ssh copy folder from local to remote and remote to local using scp and rsync on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

Leave a Reply

Your email address will not be published. Required fields are marked *