0
0
Linux CLIscripting~5 mins

SCP for file transfer in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to move files securely between computers over a network. SCP is a simple command that copies files safely using encryption, so your data stays private while moving.
When you want to copy a file from your local computer to a remote server securely.
When you need to download a file from a remote server to your local machine without exposing it to others.
When you want to transfer files between two remote servers from your local machine.
When you need a quick way to backup files to another computer on the same network.
When you want to move configuration files securely between your work laptop and home desktop.
Commands
This command copies the file 'document.txt' from your local computer to the remote computer at IP 192.168.1.10, placing it in the remote user's home directory.
Terminal
scp /home/user/document.txt user@192.168.1.10:/home/user/
Expected OutputExpected
user@192.168.1.10's password: document.txt 100% 12KB 12.0KB/s 00:01
This command downloads the file 'report.pdf' from the remote computer to your local 'Downloads' folder.
Terminal
scp user@192.168.1.10:/home/user/report.pdf /home/user/Downloads/
Expected OutputExpected
user@192.168.1.10's password: report.pdf 100% 45KB 45.0KB/s 00:01
This command copies the entire 'photos' folder and its contents recursively to the remote computer into the 'backup_photos' folder.
Terminal
scp -r /home/user/photos user@192.168.1.10:/home/user/backup_photos
Expected OutputExpected
user@192.168.1.10's password: photos/photo1.jpg 100% 2.0MB 2.0MB/s 00:01 photos/photo2.jpg 100% 3.5MB 3.5MB/s 00:02
-r - Copy directories recursively
This command copies 'script.sh' to the remote computer using port 2222 instead of the default SSH port 22.
Terminal
scp -P 2222 /home/user/script.sh user@192.168.1.10:/home/user/
Expected OutputExpected
user@192.168.1.10's password: script.sh 100% 1.2KB 1.2KB/s 00:00
-P - Specify a custom SSH port
Key Concept

If you remember nothing else from SCP, remember: it securely copies files between computers using SSH, keeping your data safe during transfer.

Common Mistakes
Forgetting to specify the remote username before the IP address.
SCP needs the username to know which account to connect to on the remote machine; without it, the command fails.
Always include the username like user@192.168.1.10 before the colon when specifying remote paths.
Trying to copy a directory without using the -r flag.
SCP copies files by default and will fail or only copy the directory as an empty file if -r is missing.
Use the -r flag to copy directories and their contents recursively.
Using lowercase -p instead of uppercase -P to specify a port.
Lowercase -p preserves file attributes, uppercase -P sets the port; using the wrong flag causes unexpected behavior.
Use uppercase -P to specify a custom SSH port.
Summary
Use scp to securely copy files between local and remote computers over SSH.
Include the remote username and IP before the colon to specify remote paths.
Use -r to copy directories and -P to specify a custom SSH port.