- Linux copy directory and contents from remote to local & vice versa
- Copy directory and files from local to remote server
- 1. Linux copy directory and files with scp recursive
- 1.1: Keep “same” directory name with scp from local to remote server
- 1.2: Change directory name with scp from local to remote server
- 2. Copy folder and files using rsync from local to remote server
- 2.1: Keep “same” directory name with rsync from local to remote server
- 2.2: Change directory name with rsync from local to remote server
- Copy directory and files from remote to local server
- 1. Linux copy directory and files with scp recursive from remote to local server
- 1.1: Keep “same” directory name with scp from remote to local server
- 1.2: Change directory name with scp from remote to local server
- 2. Copy folder and files using rsync from remote to local server
- 2.1: Keep “same” directory name with rsync from remote to local server
- 2.2: Change directory name with rsync from remote to local server
- SCP Linux Command – How to SSH File Transfer from Remote to Local
- What are SCP Commands?
- SCP Syntax
- Common SCP Commands
- Copy File From Local Host to Remote Server
- Copy Files From Remote to Local
- Copy Files From Remote Host to Another
- Copying Multiple Files
- Takeaway Points:
- Wrap 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.
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
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!!
to stay connected and get the latest updates
SCP Linux Command – How to SSH File Transfer from Remote to Local
Whenever you’re working with computers or any electronic device that has storage capacity, you might need to distribute or share information and files in various ways.
Some of the most commonly shared files include audio files, images, videos, pdfs or any form of word documents.
Most of the time, the information being shared will be private or confidential – meaning it’s meant for a specific person or a group of people, so protecting it is essential.
When it comes to devices like mobile phones we have app the facilitate the transfer of files like xender, appshare or even sometimes the use of bluetooth. Now, when it comes to computers the case is not different we have softwares and even sites that facilitate the same.
When it comes to sharing data in operating systems like Linux, there are multiple commands you can choose from to share information. But today we’ll be focusing on the SCP command. It lets you share files and data securely and easily.
In today’s market gap, having Linux skills is very essential and helpful more so if you are a system administrator. As a system admin sharing of data will be among your day to day activity and you will need the data being shared to be safe, and by using SCP command you will be able to achieve this.
Before we get started, let’s begin by understanding what SCP is, and then we’ll learn some commands you can use for file transfer.
What are SCP Commands?
SCP is an acronym for Secure Copy Protocol. It is a command line utility that allows the user to securely copy files and directories between two locations usually between unix or linux systems.
The protocol ensures the transmission of files is encrypted to prevent anyone with suspicious intentions from getting sensitive information.
In simpler words we can say that SCP is a safer option for the cp (copy) command.
It is also important to note that SCP uses encryption over an SSH (Secure Shell) connection, this ensures that the data being transferred is protected from suspicious attacks.
SCP Syntax
Just like any other commands used in the terminal, the SCP also have a format that is used for a successful execution to happen. By understanding the syntax it makes it easier for you to write down the commands:
- scp — It initializes the command and ensures a secure shell is in place.
- OPTIONS — They grant different permissions depending on how they have been used. Some of the most common options include:
- P(Caps) — specifies the port to establish connection with the remote host.
- p(lowercase) — preserves the times-tamp for ease of modification and access.
- r — copies the entire directory recursively
- q — copies files quietly, doesn’t display the progress messages. Also known as quiet mode.
- C — for compression of data during transmission.
To understand more about OPTIONS read scp options - src_host — where the file is hosted. The source can either be a client or server depending on the origin of the file.
- dest_host — where the file will be copied to.
Since we are dealing with file transmission, it definitely means that there have to be an involvement of more than one machine to make the process possible. We are able to use SCP in the following cases:
- Copy files within same machine.
- Copy files from a local host to remote host and vice versa.
- Copy files between two different remote servers.
At this point, it will be fair to state that before you use any SCP commands you will need to have a few things in place:
- SSh installed on both the client and the server machines.
- Root access to both client and server machines.
With two thing’s ready you are good to go. Let’s get started by seeing the commands in action.
Common SCP Commands
Copy File From Local Host to Remote Server
When copying files, being able to transfer files/data from local storage to remote server is very essential. When using the SCP commands you will need to specify a few things for this to happen.
You will have to specify the path to the file as the source and also specify the remote host path, where the files are being copied to.
Let’s take a scenario where we have a file test.txt and we need to copy it to a remote server, our command will look like below:
We are not limited to the number of files we can copy. Let’s say we are on our desktop in the folder called web where we have .php file extensions and we need to copy the to remote server home directory. Our command will look like:
*.php — copies all the files with the .php extension in the currently specified folder.
/
/ — means copy them to the home directory.
Let’s say you wanted to copy a file named test.txt and save it with a different name in the remote server this time round using an option of port. The command will be:
In this example we have copied a file test.txt from local machine into remote where it will be saved as test2.txt using port 8080.
Copy Files From Remote to Local
A better way to understand this is by use of an example. Take a scenario where you want to copy files from remote system. 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.
The Syntax
Let’s say I wanted to copy a file named linuxcheatsheet from the remote device with this address 192.168.1.100.
The linuxcheatsheet file is stored on the kali user’s home directory, the user I will authenticate. Therefore after the colon, I don’t need specify the path because it’s the default one, which is the home directory, and I just type the filename (“linuxcheatsheet”). Then, I specify the current directory as the local location to store the file by typing a dot.
Copy Files From Remote Host to Another
The beauty of using SCP in file transferring, is that it does not only allow connection between local machines but also it allows for you to connect to remote servers.
Let’s say we wanted to copy a file named test.txt, to another remote server the command would look like below:
What this command will do is copy test.txt from files folder in the the user1 and create a replicate of it in user2 which runs on host2.com still on the files folder.
Copying Multiple Files
When copying multiple files, all you need to do is specify the file name as the source path. for example.
The Syntax
Let’s say we wanted to copy files 1,2,3 and 4. The command would look like below:
Takeaway Points:
- To be able to copy files, you must have read permissions on the source file and write permission on the target system.
- The SCP command relies on SSH for secure data transfer, meaning it requires a password to authenticate on remote systems.
- Watch out when copying files with the same name and location, as SCP will overwrite them without warning you.
- To be able to distinguish between local and remote locations, use full colon :.
Wrap Up
Whether you are a support engineer, system admin, or even a growing developer like myself who uses Linux or wants to learn it – it’s likely that you will have to transfer files at some point. And knowing these simple SCP commands will come in handy.
In this article, we have covered some of the most common scenarios where you’d want to use SCP and hopefully you have learned something new.