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?
-
1Why wouldn't I ssh? How else would I get command line access to my server? – markstewie Feb 01 '12 at 04:35
-
6Because you can `rsync` directly from your local machine. – johnsyweb Feb 01 '12 at 04:36
-
2Oh... yes I can. I see the other answers now. Thanks. – markstewie Feb 01 '12 at 04:37
-
21This is common task for (web) development with a good answer. I don't see why it is still closed. – d.raev Dec 17 '14 at 12:51
-
6I think @d.raev was referring to the fact that the question was *closed as off topic* (if I recall correctly I flagged for this question to be *migrated* to SuperUser, where I think it fits better) rather than that there was *no accepted answer*. Still... it was nice to have my answer accepted after all this time. :-) – johnsyweb Dec 18 '14 at 04:49
-
You're right @Jonsyweb, I'm sorry... was having one of those mornings and jumped to conclusions. – markstewie Dec 18 '14 at 08:31
-
3@markstewie no bad feelings, it is a good question and it helped me (seems many others too) but this "closed" make it look not trust worthy so I wonted to bring some attention to it. – d.raev Dec 18 '14 at 11:15
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.

- 4,470
- 2
- 16
- 29

- 136,902
- 23
- 188
- 247
-
What did you intend the -h to do, the reference seems to state it will show the help file, but that makes no sense to me? – Mallow Apr 04 '14 at 22:38
-
@Mallow: Ah yes, I've seen that happen in the past (I think on SunOS). Per the link in my answer: `h output numbers in a human-readable format` – johnsyweb Apr 05 '14 at 02:03
-
Ah I see, I failed to notice the -h reference as there are two references to that flag. Ah Will be sure to use it. – Mallow Apr 08 '14 at 00:35
-
358An explanation of the command: http://explainshell.com/explain?cmd=rsync+-chavzP+--stats+user%40remote.host%3A%2Fpath%2Fto%2Fcopy+%2Fpath%2Fto%2Flocal%2Fstorage – beefsack Aug 16 '14 at 06:42
-
1What would be the user@host.remoted.from? Do I need an SSH server (and not just an SSH client) on my local machine for this to work? – CMCDragonkai Feb 22 '15 at 11:03
-
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. – johnsyweb Feb 22 '15 at 20:00
-
I found out you can run a local command in an existing SSH session. So I could potentially run the same (from the local machine) command without leaving the SSH session/terminal. – CMCDragonkai Feb 23 '15 at 02:54
-
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. – chap Oct 21 '15 at 05:22
-
23I came here for an rsync command and I came away with [explainshell.com](http://explainshell.com). Thanks @beefsack! – Cyphase Feb 23 '17 at 06:56
-
I say, all commands appear on SO should be automatically hyperlinked to explainshell :-) – HongboZhu Feb 26 '19 at 14:43
-
@Johnsyweb Do I have to have the server mounted in the local machine first? or I can pass the username and password to the rsync command? – Tak May 08 '19 at 08:19
-
@Tak: My assumption is that the "server" is remote and is listening for an SSH connection and that its shared directories are not mounted locally. In the examples I have given, the username is `user` and key / password negotiation would be handled by SSH. – johnsyweb May 08 '19 at 11:10
-
-
@Johnsyweb what is the return value of rsync when file transmission is succeefully done? and how can i catch this? – user13145713 Oct 22 '20 at 21:21
-
@user13145713: `rsync` has the conventional exit value of 0 on success (and a range of positive integers to denote different failures, as per the "EXIT VALUES" section of `man rsync`, linked from my 8½-year-old answer). See https://stackoverflow.com/a/8696712/78845 for catching it. – johnsyweb Oct 24 '20 at 03:01
-
@Akif: Using the flags in my examples, `.git/` *is* copied. There are filter options available if you wish to prevent this. – johnsyweb Oct 24 '20 at 03:07
-
@Johnsyweb is there any c-library for rsync to use?. I do not found any. – user13145713 Oct 26 '20 at 13:49
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.

- 303,325
- 100
- 852
- 1,154

- 1,314
- 1
- 10
- 24
-
7Why use `scp` rather than `rsync`? Also, watch that your shell doesn't try to expand `user@host:/path/folder/*`, perhaps by using single quotes (`'`). – johnsyweb Feb 01 '12 at 19:37
-
I find scp easier to use from the destination.... Essentially it is file copy over ssh, but with out having to establish the ssh connection first. – Tezyn Feb 02 '12 at 04:30
-
9I know what `scp` is, that wasn't my question. Why didn't you use [`rsync`](http://en.wikipedia.org/wiki/Rsync)? This is what is what the question asks for, doesn't require an established SSH session *and* is often faster and more efficient than `scp`. – johnsyweb Feb 02 '12 at 04:49
-
While the title included rsync, I read the actual question as "what would the command be to copy all files...." Lacking the ability to clarify the question with a comment, I extended my answer, taking the risk that I would be off base, and my answer would not be found useful. In my comment above, I didn't mean to come off like I was trying to educate you about scp. The point I was trying to make was "cp over ssh". I use it anywhere I would locally use cp, but between hosts. – Tezyn Feb 02 '12 at 05:30
-
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). – Mondane Jan 26 '14 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. – kyle May 01 '14 at 13:30
-
`rsync` might be preferred over `scp` **when the files involved are very large** and an update is being performed. That is, most large files were already copied once, only the differences need to be transmitted. – Serge Stroobandt Dec 01 '18 at 13:04
-
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.

- 2,102
- 2
- 16
- 29