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?

  • 14,809
  • 8
  • 18
  • 21
  • 1
    I find `scp` to be a cumber stone often. If it could suite your needs, try this out http://www.linuxjournal.com/article/8904 It mounts the remote file system locally. – TheMeaningfulEngineer Dec 24 '13 at 09:57
  • 53
    @Alan I think you mean "cumbersome"? Cumber Stone is a [Magic](http://en.wikipedia.org/wiki/Magic:_The_Gathering) card ;) – Izkata Dec 24 '13 at 14:47
  • 3
    somewhat related: [make an encrypted archive of local `dir/` on remote machine using `ssh`](http://www.pixelbeat.org/cmdline.html): `tar -c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg'` – jfs Dec 24 '13 at 20:49
  • 2
    If you has access of the ftp of the remote server, we can also use **wget** to download like `$wget -r --level=9 --no-parent --reject "index.html*" ftp://:@/path/to` [Reference1](http://kspace.in/blog/2010/02/22/copy-files-using-wget/) [Reference2](http://stackoverflow.com/a/273776/3049065) – Mahendran Sakkarai Jan 19 '15 at 06:22
  • 2
    Not duplicate, linked question specifically asks not to use `scp`; for that one `scp` is fine and most easy way as answer, so mods please remove duplicate flag as if somebody will go to other one, most useful answer will be missed out. – Alexei Martianov Oct 11 '19 at 12:13
  • @izkata I think I like "is a cumber stone" better. I will henceforth use it in place of "is cumbersome." – Isaac Rabinovitch Nov 26 '21 at 23:32

10 Answers10



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
  • 4,624
  • 10
  • 31
  • 40
  • 69,217
  • 8
  • 93
  • 130
  • 172
    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
  • @GarrettAlbright, I think the one but last `System B` should be a `System A`, right? So: "That being said, you can use `scp` if you're logged into System A via SSH and want to copy files between System B and System C." – jmc Dec 24 '13 at 17:38
  • I didn't think you could `scp` from one remote location to another remote location. I recall trying it once and being admonished by `scp`. – DopeGhoti Dec 24 '13 at 20:01
  • 1
    jmc, nope, I meant it like that. If you're shelled into System B via `ssh`, you can then execute `scp` on System B to copy files between System B and some other server ("System C"). The `scp` process will run on System B, which is the "local" system as far as it will be concerned. – Garrett Albright Dec 25 '13 at 05:41
  • 6
    @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 08 '14 at 19:48
  • Can I use this to copy to my local machine (MacBook)? And if so, where can I look to find out my MacBook's username? – Marco Prins Jan 21 '15 at 07:59
  • 1
    At any terminal prompt, you can use `whoami` to find your username. You can use SSH/SCP to copy to a Macbook, but you have to enable "Remote Login" in System Preferences -> Sharing. This will also tell you your username. – DopeGhoti Jan 21 '15 at 17:56
  • 6
    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
  • 20
    Remember -r (recursive) and -p (preserve system metadata): scp -rp – Rutrus Jul 01 '15 at 07:23
  • 2
    I am doing this from a Mac and you don't need your local username. Just the path to the file you want to copy locally and where you want it to go to remotely. Example: scp /home/myuser/sample.txt remoteuser@remoteserver.com:/home/remoteuser/ – Patrick Jul 30 '15 at 15:52
  • 1
    In your comment, you explicitly are using the username on the remote system. You _can_ omit the username if your username is the same on the remote system, or if you have settings in `.ssh/config` for that host to specify the username for you. The rules for the username for `scp` are identical to those for `ssh`. – DopeGhoti Jul 31 '15 at 17:32
  • can path/to/file be a URL to a public file on web? – Jama Aug 25 '15 at 00:19
  • No, SCP doesn't know how to handle HTTP on either the source or the destination. – DopeGhoti Feb 06 '16 at 18:55
  • Is there a way for me to specify what port to use? – Jomar Sevillejo Apr 26 '16 at 01:08
  • 18
    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
  • @GarrettAlbright you comment is very ambiguous. I understood exactly nothing. A simple explanation would be "the first argument of scp is the source and second is the destination". Done! – machineaddict Apr 29 '16 at 11:03
  • 2
    To be precise: Actually it doesn't matter where you're logged in (on your machine!), but instead it matters where the current shell you're using to run the command (!) is logged in. – Zelphir Kaltstahl Jan 15 '17 at 11:22
  • 1
    @machineaddict What my answer addresses that your "simple explanation" does not is that you do not use SCP to copy between two remote systems. Either the source or the destination must be the local machine. – Garrett Albright May 11 '17 at 01:10
  • Note to future readers of this post, an ssh server needs to be running on the `source` computer – daka May 26 '18 at 10:54
  • If you have ssh config file things are so nice. You can name your machines and do something like this - `scp -F ssh_config my-machine-one:/remote_path_to_file /loca_path_to_file` – thinklinux Dec 08 '18 at 10:20
  • @GarrettAlbright What you are missing is that in most cases a user does not know exactly where the file(s) is that they want to copy or their exact names. So, they usually have to ssh into the remote system to find that out before doing the copy. So, in fact they are usually logged into the remote system when they do the copy. – Tyler Durden Jan 11 '19 at 17:25
  • 1) Yes you can move files between two remote hosts with SCP. One is obviously going to be the source, and the other the destination. You must be logged onto the one that executes the command, one way or another. But they are still both remote. Consider Ansible, Telnet, KVM over IP. 2) You can only move a file back to the client with SCP if it is running an SSH server. – mckenzm Sep 09 '19 at 03:37

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

or, in macOS

brew install 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

On BSD and macOS, to unmount the filesystem:

umount mountpoint


diskutil unmount mountpoint

For more see here, linuxjournal.com libfuse/sshfs

Rakib Fiha
  • 562
  • 3
  • 9
  • 22
Ruban Savvy
  • 8,061
  • 7
  • 28
  • 43
  • 8
    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
  • 53
    +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
  • 14
    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 02: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 05:38
  • Note: [1] If get error `Transport endpoint is not connected` after ctrl+c cancel and retry, then simply do `fusermount -u /home/user/test`, ref: https://stackoverflow.com/a/29400722/1074998 [2] In case you already in the mount directory, you may need to `cd ..` and re-enter the directory after `mount` to see the files. – 林果皞 Jan 04 '18 at 09:15
  • comment to myself: if umount doesn't work: umount -f – DuDa Oct 14 '21 at 15:04
  • Adding "fuse" to a system is not something I would do without thinking trough. It's quiet a big dependency to just copy files from a server. – gagarine Mar 23 '22 at 13:19

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 -
Dan Garthwaite
  • 7,276
  • 1
  • 14
  • 9
  • 21
    Creativity? This is how we used to share tape drives. :) – Dan Garthwaite Dec 08 '15 at 15:01
  • 4
    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
  • 5
    @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
  • 5
    Tar will chdir to "/" thus all the following paths are relative to "/". – Dan Garthwaite Oct 29 '16 at 14:27
  • Such a subtle data transfer mechanism... thanks! – Arun Panneerselvam Mar 30 '21 at 21:18
  • This is awesome. Can confirm that this also works with Azure CLI's ssh extension https://docs.microsoft.com/en-us/cli/azure/ssh?view=azure-cli-latest#az-ssh-vm – Jujhar Singh Feb 17 '22 at 15:45

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

(see tutorial here)

  • 468
  • 3
  • 5

If they are running SCP / SSH on a different port, make sure you specify the uppercase -P port option.


scp -P 7121 /users/admin/downloads/* root@
  • 301
  • 2
  • 3
  • 2
    This question is seven years old and has many answers already. While it is true that ssh can use non-default ports and the ssh client's `-P` option is then required, you don't really help with the problem at hand. Try focusing on recent questions. – berndbausch Feb 07 '21 at 01:39
  • 4
    @berndbausch yet this comment helped me today. Thanks OP. – mombul Dec 16 '21 at 19:20

A simpler method that works with via SSH controlled NVIDIA Jetson computers is to connect with SFTP (SSH File Transfer Protocol).

Suppose I wish to move the document UFO_blueprint.odt from NASA's remote servers and move it into my Documents.

  1. cd to where you want the file saved

    $ cd Documents
  2. Connect

    $ sftp sammy@your_server_ip_or_remote_hostname
  3. Go the directory that contains the file to be transferred.

    $ cd NASA/secret_files/
  4. Transfer

    $ get UFO_blueprint.odt

To get the complete directory, instead use

$ get -r secret_files/
  • 19,340
  • 16
  • 43
  • 67
Pe Dro
  • 1,150
  • 1
  • 6
  • 13

Unix/Linux recursive copy of files and folders over a secure shell using the code following:

scp -r <host@machine_name:from_remote_directory_path> <to_local_directory_path>

A full example might look like:

scp -r sizwe@hprobook:./home/sizwe/PycharmProjects ./home/immaculate/OtherPycharmProjects

Note if you do not have a DNS server to resolve hostnames, use your IP address instead. The example above might look like this

scp -r ./home/immaculate/OtherPycharmProjects
  • 19,340
  • 16
  • 43
  • 67
  • 71
  • 1
  • 1

If speed (and not security) is your priority, then check out netcat. Before I start, let me warn you that this should be used only in a trusted network because netcat transfers are not encrypted.

First, on the receiving side, run this:

nc -lp 22222 >FileName

Then on the sending side:

nc -w3 <receiver IP or hostname> 22222 <FileName


  • -l: Listen mode
  • -p: Port number to listen on (I picked an arbitrary port)
  • -w: Connect timeout
Hopping Bunny
  • 472
  • 2
  • 3

If you have one of the common OSes, you can install pigz that is the same as gzip except that it will use multiple cores and be quicker over a fast network. My command is a bit different.

tar cf - -C /opt -S <dir> | pigz | ssh <target> "pigz -d | tar xf - -C /opt -S"

or the other way to get files

ssh <target> "tar cf - -C /opt -S <dir> | pigz" | pigz -d | tar xf - -C /opt -S

or backup the root fs with

ssh <target> "tar cf - -C / --one-file-system -S --numeric-owner . | pigz" > root.tgz
  • [And you can use ssh -J to go through a proxy server](https://unix.stackexchange.com/a/610135/209677) – Pablo A Sep 18 '20 at 20:59

ssh user@domain "printf 'dir' | pax -w " | pax -r

Use it instead of scp. Pax standardized [1] by IEEE, and scp deprecated by lwn[2] usersg.

[1] https://man.bsd.lv/pax.1#STANDARDS [2] https://lwn.net/Articles/835962/

From someone else on "the CLI-way" ;-)

  • 31
  • 1