Linux copy file remote host

How do I copy a folder from remote to local using scp?

How do I copy a folder from remote to local host using scp ? I use ssh to log in to my server.
Then, I would like to copy the remote folder foo to local /home/user/Desktop . How do I achieve this?

The OP’s question was whether it is possible to copy file from remote to local host while ssh’d to remote host. I’m not sure why no single answer has correctly addressed his/her question.

The premise of the question is incorrect. The idea is, once logged into ssh, how to move files from the logged-in machine back to the client that is logged in. However, scp is not aware of nor can it use the ssh connection. It is making its own connections. So the simple solution is create a new terminal window on the local workstation, and run scp that transfers files from the remote server to local machine. E.g., scp -i key user@remote:/remote-dir/remote-file /local-dir/local-file

@sjas: in mc it’s easier to use Left/Right on the menu > Shell link where you can type the alias you have in your ~/.ssh/config e.g. myhost: > OK

@jeffmcneill yes your right. But you didn’t address directly JeffDror, so I guess most people did not realize that your are answering JeffDror’s question.

13 Answers 13

scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/ 

By not including the trailing ‘/’ at the end of foo, you will copy the directory itself (including contents), rather than only the contents of the directory.

-r Recursively copy entire directories

Two nice-to-knows I found: the -C flag adds compression and the -c flag lets you pass in other cipher types for better performance, like scp -c blowfish a@b:something . as seen in dimuthu’s answer

This answer lacks important explanation. Will you end up with Desktop/foo or will you have Desktop/allcontentsofFooGohere scp seems to act weird sometimes to me it does one thing then another

@Toskan with scp -r user@your.server.example.com:/path/to/foo /home/user/Desktop/ you should end up with Desktop/foo . With scp -r user@your.server.example.com:/path/to/foo/. /home/user/Desktop/ you will end up with the contents of foo in Desktop and all the sub-dirs of foo strewn under Desktop

To use full power of scp you need to go through next steps:

Then, for example if you have this ~/.ssh/config:

Host test User testuser HostName test-site.example Port 22022 Host prod User produser HostName production-site.example Port 22022 

you’ll save yourself from password entry and simplify scp syntax like this:

scp -r prod:/path/foo /home/user/Desktop # copy to local scp -r prod:/path/foo test:/tmp # copy from remote prod to remote test 

More over, you will be able to use remote path-completion:

scp test:/var/log/ # press tab twice Display all 151 possibilities? (y or n) 

For enabling remote bash-completion you need to have bash-shell on both and hosts, and properly working bash-completion. For more information see related questions:

Читайте также:  Полное имя файла линукс

Источник

SCP Linux Command – How to SSH File Transfer from Remote to Local

Hillary Nyakundi

Hillary Nyakundi

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 [OPTIONS] [[user@]src_host:]file1 [[user@]dest_host:]file2 
  • 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.
Читайте также:  Writing device drivers in linux

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:

scp test.txt userbravo@destination:/location2 

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:

scp *.php userbravo@destination_host:/~/ 

*.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:

scp -P 8080 test.txt userbravo@destination_host:/user/home/test2.txt 

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.

Читайте также:  Linux команда root пароля

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.

scp lary@192.168.1.100:linuxcheatsheet .

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:

scp user1@host1.com:/files/test.txt user2@host2.com:/files

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

scp file1 file2 . user@: Destination 

Let’s say we wanted to copy files 1,2,3 and 4. The command would look like below:

scp file1.txt file2.txt file3.txt file4.txt user1@host1.com:/home/user1/Desktop

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.

Источник

Оцените статью
Adblock
detector