Once I've ssh'd into my remote server, what would the command be to copy all files from a directory to a local directory on my machine?
3 Answers
From your local machine:
rsync -chavzP --stats user@remote.host:/path/to/copy /path/to/local/storage
From your local machine with a non standard ssh port:
rsync -chavzP -e "ssh -p $portNumber" user@remote.host:/path/to/copy /local/path
Or from the remote host, assuming you really want to work this way and your local machine is listening on SSH:
rsync -chavzP --stats /path/to/copy user@host.remoted.from:/path/to/local/storage
See man rsync
for an explanation of my usual switches.
-
351
-
164
-
2@cmcdragonkai: indeed, the local host must be running an ssh server and be accessible to the remote host. This is one of the reasons that I prefer the first solution over the second. Feb 22, 2015 at 20:00
-
26Be careful when rsyncing with trailing slashes. The command given by Johnnysweb would create a directory called copy inside
/path/to/local/storage
. Like so/path/to/local/storage/copy
. If that's what you want great. However a more common scenario is you want to copy the contents of the remote directory into a directory in your local. Then you would do/path/to/copy/
which would place the contents inside the directory/path/to/local/storage
without creating a local copy directory.– chapOct 21, 2015 at 5:22 -
23I came here for an rsync command and I came away with explainshell.com. Thanks @beefsack!– CyphaseFeb 23, 2017 at 6:56
If you have SSH access, you don't need to SSH first and then copy, just use Secure Copy (SCP) from the destination.
scp user@host:/path/file /localpath/file
Wild card characters are supported, so
scp user@host:/path/folder/* /localpath/folder
will copy all of the remote files in that folder.If copying more then one directory.
note -r will copy all sub-folders and content too.
-
7Why use
scp
rather thanrsync
? Also, watch that your shell doesn't try to expanduser@host:/path/folder/*
, perhaps by using single quotes ('
). Feb 1, 2012 at 19:37 -
8
-
27Remember, scp follows symlinks instead of copying them. This can lead to copying more then you expect and in loss of symlinks (they become normal folders/files).– MondaneJan 26, 2014 at 10:59
-
2Mondane's response is precisely the reason I found this post. Normally, I use scp for everything, but I needed to preserve permissions and symlink from server to server. Also, the speed difference (bc my newbie self definitely scped first) was vast.– kyleMay 1, 2014 at 13:30
-
1
I think it is better to copy files from your local computer, because if files number or file size is very big, copying process could be interrupted if your current ssh session would be lost (broken pipe or whatever).
If you have configured ssh key to connect to your remote server, you could use the following command:
rsync -avP -e "ssh -i /home/local_user/ssh/key_to_access_remote_server.pem" remote_user@remote_host.ip:/home/remote_user/file.gz /home/local_user/Downloads/
Where v
option is --verbose
, a
option is --archive
- archive mode, P
option same as --partial
- keep partially transferred files, e
option is --rsh=COMMAND
- specifying the remote shell to use.
rsync
directly from your local machine.