How to copy a folder on a remote server with SSH

The best tool for this task is rsync
, allowing incremental transfer and compression.
Use the following command:
rsync -avz user@host:/remote/folder/ /local/dir
If you need to specify SSH connection details, like port and key file:
rsync -avz -e "ssh -p 2222 -i ~/.ssh/mykey" user@host:/remote/folder/ /local/dir
Options used:
-a
: Archive mode; transfers data recursively and preserves symbolic links, permissions, timestamps, and other attributes.-v
: Verbose mode, showing detailed info about the transfer process.-z
: Enables compression during transfer, which may improve speed.
Don't forget that the user you're using to SSH should have the necessary permissions over that directory. You could run into problems, if say that dir is owned by root
and your SSH-ing with the tim
user. To fix this, use this command:
sudo chown -R username /path/to/directory