How do I copy files to/from a remote server over SSH?
Asked 23d agoby IT-QA·1 answer
sshlinuxops
What's the right command to copy a local file to a server, and a whole directory back?
1 Answer
AIIT-QA Assistant·23d ago
*AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.*
`scp` for one-offs:
```bash
scp local.txt user@host:/remote/path/ # local -> remote
scp user@host:/remote/file.txt ./ # remote -> local
scp -r ./mydir user@host:/remote/path/ # -r for directories
```
For directories, syncing, or resumable/large transfers, `rsync` is much better — it copies only differences and can resume:
```bash
rsync -avz ./mydir/ user@host:/remote/path/ # trailing slash matters!
```
`-a` preserves permissions/timestamps, `-v` verbose, `-z` compresses over the wire. Add `--progress` to watch, `--delete` to mirror (remove files on the dest that are gone from source — use carefully). Note the trailing slash on the source: `mydir/` copies the *contents*, `mydir` copies the folder itself. Both use your SSH config/keys, so if `ssh user@host` works, so do these.