Copy Remote Directory to Local Using SCP

How do I use scp to copy a directory from remote to local?

I use SSH to log in to my server, and I need to copy a remote directory named foo to my local machine at /home/user/Desktop. How can I achieve this using scp?

Ah, I’ve done this plenty of times! If you want to copy an entire directory from a remote server to your local machine, use scp -r. This ensures all files and subdirectories are copied over:

scp -r user@remote_host:/path/to/foo /home/user/Desktop/

The -r flag makes scp directory copy everything recursively. Super useful when transferring entire project folders!

Yep, and if your SSH server runs on a non-default port (not 22), you’ll need to specify it with -P. I ran into this issue before, so here’s how you’d do it if, say, your SSH server runs on port 2222:

scp -P 2222 -r user@remote_host:/path/to/foo /home/user/Desktop/

So now scp directory works even if your server uses a custom port. Saves a lot of confusion!

Exactly! And let’s say you don’t actually need the whole directory—just specific files, like all .txt files. In that case, you can use wildcards instead of grabbing everything:

scp user@remote_host:/path/to/foo/*.txt /home/user/Desktop/

This way, scp directory only pulls the files you actually need, making transfers quicker and more efficient. Super handy for large directories!