Linux copy file from remote host

Содержание
  1. Linux copy directory and contents from remote to local & vice versa
  2. Copy directory and files from local to remote server
  3. 1. Linux copy directory and files with scp recursive
  4. 1.1: Keep «same» directory name with scp from local to remote server
  5. 1.2: Change directory name with scp from local to remote server
  6. 2. Copy folder and files using rsync from local to remote server
  7. 2.1: Keep «same» directory name with rsync from local to remote server
  8. 2.2: Change directory name with rsync from local to remote server
  9. Copy directory and files from remote to local server
  10. 1. Linux copy directory and files with scp recursive from remote to local server
  11. 1.1: Keep «same» directory name with scp from remote to local server
  12. 1.2: Change directory name with scp from remote to local server
  13. 2. Copy folder and files using rsync from remote to local server
  14. 2.1: Keep «same» directory name with rsync from remote to local server
  15. 2.2: Change directory name with rsync from remote to local server
  16. Related Posts
  17. 4 thoughts on “Linux copy directory and contents from remote to local & vice versa”
  18. How to copy and transfer files remotely on Linux using scp and rsync
  19. Commands to copy and transfer files remotely on Linux
  20. How to us scp command to securely transfer files to remote host
  21. How to copy a local file to a remote system with the scp command
  22. Copying a remote file to a local system using the scp command
  23. Recursively copy entire directories between two hosts
  24. Using wildcards to copy multiple files
  25. How to copy a file between two remote Linux or Unix servers using the scp command
  26. How to deal with SSH port
  27. What is rsync?
  28. How to use rsync to copy and transfer files remotely on Linux and Unix
  29. Summing up

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

Table of Contents

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

  • You can use either scp or rsync to copy folder and files from local to ssh or copy folder and files from ssh to local within in the same or different directory.
  • By default copy files and folders happen sequentially. If you wish to copy directory and contents in parallel then you must use pscp or pssh tool.
  • You can also configure password less copy from local to remote or from remote to local, so you don’t have to provide password every time you try to copy files and folder between servers. This is very useful for automation.

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:

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.

Читайте также:  Windows ж йесіндегі ж мыс істеу негіздері

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

The ssh copy file from local to remote was successful. Next validate the transfer on the remote server

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:

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

All files under directory /tmp/deepak are successfully copied to remote server. Validate the content on server2 node.

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:

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

Follow rsync man page for more details. Here,

After rsync copy, validate the transfer on the remote server. So the directory name is same on local and remote server after transfer.

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:

Next execute the command in the below syntax

After the transfer, validate the content on the remote node

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

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:

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

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.

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 )

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:

We will use this syntax in our next scp example:

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

Читайте также:  Вся линейка windows server

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:

Using below command you can folder from remote to local server

Verify the content of /tmp/deepak on server1

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:

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

Validate the content on server1 under /tmp/rahul

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.

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

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

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.

Thanks for highlighting, the syntax reference was incorrect. I have corrected it.

Thanks for the tutorial. But I think “/” is forward slash, NOT backslash.

I always get confuse with this 🙂
Thank you for highlighting this. I have corrected the article.

Источник

How to copy and transfer files remotely on Linux using scp and rsync

I n Linux, how do I copy and transfer files remotely between two Linux machines? What command do I need to use to transfer files between Unix/macOS and Linux computers securely?

In Linux and Unix-like systems, you need to use the scp command or rsync command to copy files and directories between remote machines securely. This page explains how to use the scp/rsync command to copy and transfer files securely.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements scp and rsync
Est. reading time 6 minutes

Commands to copy and transfer files remotely on Linux

The scp command uses SSH protocol to transfer files. Therefore, it would be best if you had a password or passphrase or ssh keys for authentication. Both scp and rsync encrypt the file and any passwords exchanged. In other words, no one can snoop on the network to view your data or files.

How to us scp command to securely transfer files to remote host

The syntax is as follows:

  1. user1@source_A:dir1/file1 – Source file/dir.
  2. user2@destination_B:dir2/file2 – Destination file/dir.
  3. : – The colon (:) plays a vital role, and scp uses that to differentiate between local and remote locations. Would you mind making sure to include a space between the source and destination files?

Don’t worry. It is easy. Let us see some common examples.

How to copy a local file to a remote system with the scp command

To copy a file named log.txt from your current directory on Linux or macOS desktop to a directory called

/data ( $HOME/data or /home/vivek/data ) in your account on the remote system named nas04 (IP 192.168.2.17), enter:

I assume your username is vivek , and the directory called /home/vivek/data/ already exits on the nas04 server for the above examples. You can verify it using the following simple command:
ssh 192.168.2.17 — ls -l

/data/
That is all, and here is how it looks on my terminal:

How to use scp command to securely transfer files from Linux desktop to FreeBSD remote server

Copying a remote file to a local system using the scp command

Say you need to copy a file from a remote server hosted at Linode or AWS, then syntax is:

For example, copy a file named /var/www/html/config.php from a remote server with IP 1.2.3.4 run the following command:

Recursively copy entire directories between two hosts

To copy a directory and all the files it contains, please use scp command with the -r option as follows:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

The above command copy the entire

/projects/python directory from your account to your backup server IP address 192.168.2.17 account. Say at some point you deleted

/projects/python on your macOS or Linux desktop. Can we copy all files from the backup server? Yes, that is the whole point, and the syntax is reversed as follows:

Using wildcards to copy multiple files

To copy multiple files within a directory, you can use wildcards. For instance, to copy all the ‘.pl’ (Python) files from the appA directory on your EC2 server account, enter:

How to copy a file between two remote Linux or Unix servers using the scp command

For the following example, assume vivek user logged into another Linux or macOS system say $ . Now, to copy auth.py from your home directory on Box1 to your /app/auth/ directory on Box2 , enter:

Please note that you will be prompted to enter two passwords. One for the source system called Box1 and one for the destination system named Box2 . The command won’t work unless you correctly enter both passwords or ssh key passphrase. You can also state the ssh port or do a recursive copy too:

How to deal with SSH port

Say the remote host is listening on a port other than the default SSH TCP port # 22, then you need to specify the port to connect to on the remote host as follows using the -P option:

What is rsync?

Rsync is a command-line utility that provides quick incremental file transfer on Linux and Unix. In addition, Rsync recommends when you need to transfer many files due to advanced options. Also, the scp protocol is outdated as per OpenSSH and it is inflexible and not readily fixed. Hence, they recommend the use of more modern protocols like sftp and rsync for file transfer instead.

How to use rsync to copy and transfer files remotely on Linux and Unix

The rsync command has tons of options, but the most typical usage is:

In this following example, I am copying /source/dir/ into /target/dir:

Now we can do file transfer over the network as follows:

We need to preface the destination directory with the remote computer name to copy files. Again, you will be prompted for your ssh password or ssh key passphrase on the remote host before the copy starts. The following command synchronizes the directory app1 from the desktop to

vivek/data/app1 (/home/vivek/data/app1 dir) on ec2-server:

The following command synchronizes the directory

vivek/appB (/home/vivek/appB) on serverC to ./appB on the local Linux desktop in the current dir:

The rsync command options are as follows:

  • -a : archive mode (copies all sub-dirs and files recursively and preserves as many file attributes as possible)
  • -v : Verbose mode (show file names when copying they are copied)
  • -P : Show progress bar
  • —delete : Ask rsync to delete extraneous files from the receiving side (ones that aren’t on the sending side), but only for the directories that are being synchronized.

See the rsync command documentation for more info.

Summing up

You learned how to use scp or rsync to securely transfer files between two Linux or Unix systems. Please see the following man pages using the man command:
man scp
man rsync
man ssh

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Читайте также:  Просим вас указать текущий пароль windows что это
Оцените статью