Keywords: SSH | scp command | file copying | Mac OS X | terminal operations
Abstract: This article provides a comprehensive guide on using SSH protocol to copy files and directories across computers in Mac OS X Terminal environment. Addressing common errors like 'not a regular file', it details the correct usage of scp command, including basic file copying, directory transfers, and port specifications. With complete code examples and step-by-step instructions, users can master secure remote file transfer techniques.
Problem Background and Challenges
When migrating computers or backing up files in Mac OS X environments, users often need to transfer configuration files such as .profile, .rvm, and .ssh directories to a new computer. However, directly using the cp command results in a “not a regular file” error, as these targets may be directories or special files, and cp by default only handles regular files.
Solution: Basics of the scp Command
The scp (Secure Copy) command is a secure file transfer tool based on the SSH protocol, capable of copying files and directories across computers. Its syntax extends from the cp command, supporting references to remote hosts. The basic syntax is: scp [options] source destination, where source and destination can include usernames and hostnames.
Specific Operational Steps
The command to copy a file from the local computer to a remote computer is: scp /path/to/local/file username@hostname:/path/to/remote/file. For example, to copy the local .profile file to the home directory of a remote host: scp ~/.profile user@192.168.1.100:~/.profile.
The command to copy a file from a remote computer to the local computer is: scp username@hostname:/path/to/remote/file /path/to/local/file. For instance, to download the remote .ssh directory locally: scp -r user@example.com:~/.ssh ~/, where the -r option is used for recursive directory copying.
Advanced Configuration and Error Handling
When the SSH service uses a non-standard port, specify the port number with the -P option: scp -P 2222 user@hostname:/remote/file /local/path. For the “not a regular file” error, ensure the -r flag is used for directories and verify path permissions.
Security Considerations
When using scp, data transmission is encrypted via SSH, ensuring security. It is recommended to configure SSH key authentication to avoid password entry and regularly check network connections to prevent man-in-the-middle attacks.