This question already has an answer here:

I'm using Linux (centos) machine, I already connected to the other system using ssh. Now my question is how can I copy files from one system to another system?

Suppose, in my environment, I have two system like System A and System B. I'm using System A machine and some other using System B machine.

How can I copy a file from System B to System A? And, copy a file from System A to System B?

marked as duplicate by Gilles, slm, jasonwryan, Anthon, Bernhard Dec 25 '13 at 7:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



scp <source> <destination>

To copy a file from B to A while logged into B:

scp /path/to/file username@a:/path/to/destination

To copy a file from B to A while logged into A:

scp username@b:/path/to/file /path/to/destination
  • 89
    To clarify, you typically don't use scp to copy a file to or from your local machine (System A) while logged in to a remote server (System B) with ssh. scp will log you into the remote server, copy the file, then log you out again in one process, so just run it from a shell on your local machine. That being said, you can use scp if you're logged into System B via SSH and want to copy files between System B and System C. – Garrett Albright Dec 24 '13 at 16:40
  • 2
    @DopeGhoti. Yes, you can't move files between two remote computers. Either the source or destination must be a local file. However, if you log in to a remote machine with ssh, you can copy files between two remote machines on that machine's command-line. – Gee-Bee Aug 8 '14 at 19:48
  • 2
    Also, an important thing to remember is that you can only scp to a folder on the target machine to which you have permissions. If you are trying to copy it to a destination to which the target user does not have permission, first copy the file to the user's home directory or sub directory and then ssh into the target machine and sudo move it over to the final destination – shaveenk Apr 25 '15 at 20:30
  • 6
    Remember -r (recursive) and -p (preserve system metadata): scp -rp <file1> <file2> – Rutrus Jul 1 '15 at 7:23
  • 6
    Yes, use -P to specify the TCP port on the remote host. This catches me every time, because ssh uses -p. – DopeGhoti Apr 28 '16 at 18:22

In case if you need an alternate approach.

Install sshfs. if you use ubuntu/debian:

sudo apt-get install sshfs

or, if you use centos/rhel:

sudo yum install fuse-sshfs

Create an empty dir

mkdir /home/user/testdir

"link" or "mount" the two directories

sshfs user@server.com:/remote/dir /home/user/testdir

"unlink" the dirs

fusermount -u /home/user/testdir

For more see here, linuxjournal.com

  • 4
    The question specifically states that the OS in question is CentOS; not Debian-derived. apt-get is a Debianism; the correct package manager would be yum or, for really old flavors of CentOS, up2date or rpm. – DopeGhoti Dec 24 '13 at 21:30
  • 26
    +1 the answer for this question may be off context, but it still helped me, and as far as I can see, others too. – osirisgothra Jul 23 '14 at 15:27
  • 7
    Not related, but it's a one million dollar answer xD – Ramy Al Zuhouri May 15 '15 at 16:55
  • If I understand correctly, this gives ACCESS to the remote files, but does not COPY them? – ToolmakerSteve Apr 20 '16 at 2:46
  • @ToolmakerSteve you're perfectly right: it creates a fake directory (mount), so that you can use all your preferred tools to copy or move things: cp, mv, rm, rsync, unison... The scp command would only be able to copy files. – Yvan Oct 19 '16 at 5:38

Sometimes you need to get fancy with tar:

tar -C / -cf - \
  opt/widget etc/widget etc/cron.d/widget etc/init.d/widget \
  --exclude=opt/widget/local.conf | 
  ssh otherhost tar -C / -xvf -
  • 12
    Creativity? This is how we used to share tape drives. :) – Dan Garthwaite Dec 8 '15 at 15:01
  • Can you tell us what this does? For example I have no idea what 'cron' has to do with copying file! – aliqandil Oct 28 '16 at 15:00
  • 2
    @aliqandil opt/widget etc/widget etc/cron.d/widget etc/init.d/widget are individual files to be included in the tar archive. The point was to illustrate why sometimes you need to get fancy. This series of commands streams the tar archive over an ssh connection and unarchives it on the other side. Thus no tar archive is ever written to disk. I use this sometimes to cherry pick files to copy in one command. – Dan Garthwaite Oct 28 '16 at 18:51
  • Ow! I taught the first '/' was the source path! (What is it?) So that was a silly question! Thanks. :) – aliqandil Oct 28 '16 at 21:01
  • 3
    Tar will chdir to "/" thus all the following paths are relative to "/". – Dan Garthwaite Oct 29 '16 at 14:27

If you want to keep the files on both systems in sync then have a look at the rsync program:

(see tutorial here)

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