488

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
  • 1
    Why wouldn't I ssh? How else would I get command line access to my server? Commented Feb 1, 2012 at 4:35
  • 6
    Because you can rsync directly from your local machine. Commented Feb 1, 2012 at 4:36
  • 6
    I 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. :-) Commented Dec 18, 2014 at 4:49

3 Answers 3

868

From your local machine:

rsync -chavzP --stats [email protected]:/path/to/copy /path/to/local/storage

From your local machine with a nonstandard SSH port:

rsync -chavzP -e "ssh -p $portNumber" [email protected]:/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 [email protected]:/path/to/local/storage

Where...

  • -c enables checksum-based file comparison, which is more thorough than just comparing file sizes and modification times
  • -h outputs file sizes in human-readable format (e.g., KB, MB, GB)
  • -a enables archive mode - preserves symbolic links, permissions, timestamps, owner, and group
  • -v provides verbose output with detailed information about the file transfer process
  • -z enables compression during file transfer to reduce network bandwidth usage
  • -P combines two switches:
    • --partial keeps partially transferred files
    • --progress shows transfer progress
  • -e "ssh -p $portNumber" specifies the remote shell to use (SSH) with a custom port number

See man rsync for a deeper explanation of these switches.

Sign up to request clarification or add additional context in comments.

18 Comments

An explanation of the command: explainshell.com/…
What would be the [email protected]? Do I need an SSH server (and not just an SSH client) on my local machine for this to work?
@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.
Be 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.
@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 stackoverflow.com/a/8696712/78845 for catching it.
|
66

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.

8 Comments

Why use scp rather than rsync? Also, watch that your shell doesn't try to expand user@host:/path/folder/*, perhaps by using single quotes (').
I know what scp is, that wasn't my question. Why didn't you use 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.
Remember, 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'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.
Also rsync is much faster than scp.
|
8

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 man page

1 Comment

How to use variable for key path like "ssh -i $VARIABLE read here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.