NeuroAgent

How to Copy Folder with SCP Command: Complete Guide

Learn the exact scp command syntax to copy folders from remote servers to your local machine. Includes recursive flag usage, authentication options, and troubleshooting tips for successful file transfers.

Question

How do I copy a folder from a remote server to my local machine using the scp command? I use SSH to log in to my server and need to copy a remote folder named ‘foo’ to my local directory ‘/home/user/Desktop’. What is the correct scp command syntax to achieve this folder transfer?

NeuroAgent

To copy a folder from a remote server to your local machine using scp, you need to use the -r (recursive) flag. The correct command syntax for your specific case would be: scp -r username@hostname:/path/to/foo /home/user/Desktop. This command will recursively copy the entire ‘foo’ folder and all its contents from the remote server to your local Desktop directory.

Contents

Basic SCP Command Syntax

The fundamental syntax for copying a directory from a remote server to your local machine using scp follows this pattern:

scp -r [user@]hostname:/path/to/remote/directory /path/to/local/directory

Breaking down this syntax:

  • scp: The secure copy command
  • -r: Required flag for recursive directory copying
  • [user@]hostname: Your username and the remote server’s hostname or IP address
  • :/path/to/remote/directory: The absolute path to the folder you want to copy on the remote server
  • /path/to/local/directory: The local destination directory where you want to save the folder

Important: When copying from remote to local, the remote path comes first and the local path comes second in the command structure.

Complete Command Example

Based on your specific requirements of copying a folder named ‘foo’ to ‘/home/user/Desktop’, here’s the complete command you would use:

bash
scp -r username@hostname:/path/to/foo /home/user/Desktop

Practical Examples

From the research findings, here are some real-world examples:

  1. Basic remote to local folder copy:

    bash
    scp -r [email protected]:~/folderinremoteuserdir ~/folderinlocaluserdir
    
  2. Using SSH key for authentication:

    bash
    scp -r -i /path/to/local/key [email protected]:/path/to/folder /your/local/target/dir
    
  3. Copying with specific port:

    bash
    scp -P 2222 -r [email protected]:/path/to/folder /home/user/Desktop
    

Common SCP Options

The scp command supports several useful options that can enhance your file transfer experience:

Option Description Example Usage
-r Recursive copy (essential for directories) scp -r user@host:/path/to/dir /local/path
-p Preserve file permissions and timestamps scp -p -r user@host:/path/to/dir /local/path
-q Quiet mode (no progress meter) scp -q -r user@host:/path/to/dir /local/path
-P Specify port number (capital P) scp -P 2222 -r user@host:/path/to/dir /local/path
-i Identity file (SSH key) scp -i ~/.ssh/key -r user@host:/path/to/dir /local/path
-C Compression scp -C -r user@host:/path/to/dir /local/path
-v Verbose mode (debugging) scp -v -r user@host:/path/to/dir /local/path

Preserving File Attributes

If you want to maintain file metadata like ownership and creation dates, use the -p option as mentioned in the Eukhost documentation:

bash
scp -p -r username@hostname:/path/to/foo /home/user/Desktop

Using SSH Keys

For more secure connections, especially in automated scripts, specify your SSH key:

bash
scp -r -i ~/.ssh/your_key username@hostname:/path/to/foo /home/user/Desktop

Troubleshooting Common Issues

Authentication Problems

If you encounter “Too many authentication failures”, specify the exact SSH key as mentioned in the Stack Overflow answer:

bash
scp -r -i /path/to/local/key username@hostname:/path/to/foo /home/user/Desktop

Permission Denied

Ensure:

  1. Your SSH key is properly set up on the remote server
  2. You have read permissions for the source directory
  3. The local destination directory exists and you have write permissions

Symbolic Links

Be aware that scp follows symbolic links encountered during traversal. If you want to avoid this, some users prefer using rsync instead.


Alternative Methods

Using Wildcards

To copy all files from a remote directory at once, you can use wildcard patterns as mentioned in the Warp documentation:

bash
scp username@hostname:/path/to/directory/* /home/user/Desktop

Using rsync

For more advanced features like resuming transfers and better handling of large directories, consider using rsync:

bash
rsync -avz username@hostname:/path/to/foo /home/user/Desktop

Copying Multiple Directories

You can copy multiple directories by specifying them in a single command:

bash
scp -r username@hostname:/path/to/dir1 username@hostname:/path/to/dir2 /home/user/Desktop

Conclusion

Key takeaways for copying folders from remote to local using scp:

  1. Always use the -r flag when copying directories - this enables recursive copying of the entire folder structure
  2. Remember the syntax order: remote path first, local path second when copying from remote to local
  3. Use SSH keys for more secure and automated file transfers
  4. Consider additional options like -p for preserving metadata or -P for specifying custom ports
  5. For large or complex transfers, rsync might be a better alternative than scp

The complete command for your specific case remains: scp -r username@hostname:/path/to/foo /home/user/Desktop

Sources

  1. How do I copy a folder from remote to local using scp? - Stack Overflow
  2. Unix & Linux Stack Exchange - Copying files from remote to local
  3. Warp - Copying files from remote to local
  4. StackAbuse - Copying a Directory with SCP
  5. Howtogeek - Copying directories recursively with scp
  6. Eukhost - SCP command for directory transfers
  7. Linuxize - Using SCP command
  8. Indiana University Knowledge Base - SCP command