copy file/folder using scp command
Categories:
Mastering SCP: Securely Copying Files and Folders
Learn how to use the scp
command to securely transfer files and entire directories between local and remote Linux or Windows systems.
The scp
(Secure Copy Protocol) command is a powerful and widely used tool for securely transferring files and directories between hosts on a network. It leverages SSH (Secure Shell) for data transfer and authentication, ensuring that your data remains encrypted and protected during transit. This article will guide you through the essentials of using scp
for various file and folder transfer scenarios, including between local and remote machines, and between two remote machines.
Understanding SCP Basics and Syntax
At its core, scp
operates with a simple syntax: scp [OPTIONS] [SOURCE] [DESTINATION]
. Both the source and destination can be local paths or remote paths specified in the format user@host:path
. The scp
command requires SSH to be running on the remote server and that you have appropriate authentication credentials (password or SSH keys) for the remote user.
flowchart LR A["Local Machine"] -- "scp command" --> B["Remote Machine (SSH)"] B -- "Secure Data Transfer" --> C["Destination Path"] A -- "Secure Data Transfer" --> C subgraph SCP Process A -- "Authentication" --> B B -- "File/Folder Copy" --> C end
Basic SCP file transfer process
Copying Files
Copying individual files is the most common use case for scp
. You can transfer files from your local machine to a remote server, from a remote server to your local machine, or even directly between two remote servers.
~
refers to the home directory of the user on the remote machine. If the path contains spaces, enclose it in quotes.Local to Remote
scp /path/to/local/file.txt user@remote_host:/path/to/remote/directory/
# Example: scp ~/documents/report.pdf john@example.com:/home/john/reports/
Remote to Local
scp user@remote_host:/path/to/remote/file.txt /path/to/local/directory/
# Example: scp john@example.com:/var/log/syslog.log ~/logs/
Remote to Remote
scp user1@host1:/path/to/file.txt user2@host2:/path/to/directory/
# Note: This requires authentication for both remote hosts from your local machine.
Copying Directories (Folders)
To copy entire directories, you need to use the -r
(recursive) option. This tells scp
to copy the directory and all its contents, including subdirectories and files.
scp -r /path/to/local/folder user@remote_host:/path/to/remote/destination/
# Example: scp -r ~/projects/my_app/ deploy@webserver.com:/var/www/html/
scp -r user@remote_host:/path/to/remote/folder /path/to/local/destination/
# Example: scp -r admin@backup.server.com:/backups/db_data/ ~/local_backups/
Using the -r
option for recursive directory copying
scp -r
with large directories, as it can consume significant network bandwidth and disk space on the destination. Ensure you have sufficient permissions on both source and destination.Useful SCP Options
Beyond basic file and folder transfers, scp
offers several options to enhance functionality and control over the transfer process.
# Preserve modification times, access times, and modes
scp -p file.txt user@remote_host:~
# Limit bandwidth (e.g., to 1000 Kbit/s)
scp -l 1000 large_file.zip user@remote_host:~
# Use a specific SSH key for authentication
scp -i ~/.ssh/my_private_key file.txt user@remote_host:~
# Specify a different SSH port (default is 22)
scp -P 2222 file.txt user@remote_host:~
# Verbose output for debugging
scp -v file.txt user@remote_host:~
Commonly used scp
options
1. Verify SSH Access
Before using scp
, ensure you can connect to the remote host via SSH. Try ssh user@remote_host
to confirm connectivity and authentication.
2. Determine Source and Destination Paths
Clearly identify the full path of the file or folder you want to copy (source) and the exact location where you want to place it on the target machine (destination).
3. Construct the SCP Command
Based on whether you're copying a file or a folder, and the direction of transfer, assemble your scp
command, including any necessary options like -r
for directories or -P
for custom ports.
4. Execute and Authenticate
Run the command. If prompted, enter your password for the remote user or ensure your SSH keys are correctly set up for passwordless authentication.
5. Verify Transfer
After the command completes, log into the destination machine and verify that the file or folder has been copied correctly to the specified location.