209

In my terminal shell, I ssh'ed into a remote server, and I cd to the directory I want.

Now in this directory, there is a file called table that I want to copy to my local machine /home/me/Desktop.

How can I do this?

I tried scp table /home/me/Desktop but it gave an error about no such file or directory.

Does anyone know how to do this?

  • If you find yourself copying with scp often, you can mount the remote directory in your file browser and drag-and-drop. On my Ubuntu 15 host, it's under the menu bar "Go" > "Enter Location" > debian@10.42.4.66:/home/debian. Alternatively, one can use sshfs to mount the remote machine's filesystem on the host. But that setup is a little more involved. – ConvexMartian Nov 28 '16 at 19:46
  • Give rsync a try. It's great both for local and remote copies, gives you copy progress, etc. An example – sakisk Jun 8 '17 at 19:23
345

The syntax for scp is:

If you are on the computer from which you want to send file to a remote computer:

scp /file/to/send username@remote:/where/to/put

Here the remote can be a FQDN or an IP address.

On the other hand if you are on the computer wanting to receive file from a remote computer:

scp username@remote:/file/to/send /where/to/put

scp can also send files between two remote hosts:

scp username@remote_1:/file/to/send username@remote_2:/where/to/put

So the basic syntax is:

scp username@source:/location/to/file username@destination:/where/to/put

You can read man scp to get more ideas on this.

  • 4
    What if I want to copy multiple files? I added a space and just used another /file/to/send Thanks for your awesome answer! – camdixon Jul 12 '16 at 18:30
  • 5
    scp -r will copy recursively. – Henry Aug 10 '16 at 21:10
  • What i want to copy the files from network to my VM ...how to achieve the same using scp – Sushivam Nov 16 '16 at 12:48
  • 1
    @heemayl +1 for the good answer. Thought to add that in the case that it is a secure connection (that requires an authentication) you can use the following (for copying file from local to remote): scp -i mykey.pem somefile.txt root@remotehost.ip.address:/some/folder/in/remote – Guy Avraham Oct 19 '17 at 10:48
  • 1
    Use scp -P 123 to specify custom port – aexl Dec 28 '17 at 12:52
14

You can use rsync as an alternative. It is mainly for syncing files.. but you can use it for this purpose as well.

rsync -avzh --stats --progress remoteuser@remoteip  localpath 

to add ssh options:

rsync -e "ssh -P $port_value" remoteuser@remoteip  localpath

--progress and --stats are useful for real-time display of transfer.

9
scp root@10.240.179.4:/root/Jmeter/reports.jtl Downloads/
  • 3
    I like this kind of answer. – xtluo Jul 3 '18 at 8:05
7
scp username@ipaddress:pathtofile localsystempath

scp sadananad@ipaddress:/home/demo/public_html/myproject.tar.gz .

If your using with port:

scp -Pportnumber username@ipaddress:pathtofile localsystempath 

scp -P2233 sadananad@ipaddress:/home/demo/public_html/myproject.tar.gz .
  • This will copy the file to the same remote directory. – Max Yudin Oct 12 '18 at 11:02
3

If you completely trust everyone in the network and you can connect a port of the destination machine directly, you can use netcat: nc.

Let's say the IP address of the destination machine is 192.168.1.123

On the destination run:

nc -l -p 7777 0.0.0.0 | tar zxvf - -C dest_dir

You can choose a different port, and also bind to another IP of your interfaces, 0.0.0.0 just catches on all interfaces.

On the source run:

tar zxcf - filename | nc 192.168.1.123 7777

IMHO, this is the fastest possible way to send a file from one computer to another using digital networks.

The arguments and command line options might slightly change between different versions of nc and tar, but it will definitely work with recent Linux distributions.

2

On Linux, to copy a folder and its content from the user (root in this example) directory, to a folder in the local user directory, I run this command on the local machine:

scp -r root@178.62.54.83:~/folderinremoteuserdir ~/folderinlocaluserdir

Note the ~/ which I often seem to forget...

protected by Community Aug 12 '16 at 9:55

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.