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

I’m trying to figure out how to use the scp folder command properly. I can SSH into my remote server without any issues, but now I want to copy a folder named foo from the remote machine to my local directory (/home/user/Desktop).

What’s the correct syntax for copying an entire folder using scp?

Do I need any special flags to ensure all contents and subdirectories are copied properly?

I’ve had to do this often when backing up logs from remote servers.

To copy a folder with all its contents, make sure to use the -r (recursive) flag with scp.

Here’s the command that always works for me:

bash
Copy
Edit
scp -r user@remote:/path/to/foo /home/user/Desktop

This grabs the entire foo directory, including all subfolders and files, and places it right on your desktop.

Just replace user and remote with your actual SSH username and server address. Also, if you’re on a different port (say 2222), don’t forget to add -P 2222.

Hope this helps!

I remember struggling with this exact thing when setting up automated backups.

One important tip: always make sure the remote path is absolute or that your shell can resolve it correctly.

I usually write it like this:

bash
Copy
Edit
scp -r "user@192.168.1.100:/home/user/foo" ~/Desktop/
Quoting the remote path avoids issues with spaces or weird characters, and using ~ locally saves time. Also, if your folder is huge or the connection isn't great, consider combining it with -C to enable compression:

bash
Copy
Edit
scp -rC user@remote:/path/to/foo ~/Desktop/

That’s shaved a few minutes off big transfers for me. Try it out!