Need to move files between your machine and a server? scp (Secure Copy) is the tool I reach for most often. It uses SSH under the hood, so everything’s encrypted—both the files and your password.
You can copy in any direction: local to remote, remote to local, or even between two remote machines.
Basic Syntax
scp [OPTIONS] source destination
For remote locations, you specify user@host:path. Local files just use regular paths.
The most useful options:
-P— SSH port (note: uppercase, unlike ssh’s lowercase-p)-r— Recursive, for directories-p— Preserve modification times-C— Compress during transfer-q— Quiet mode, suppresses the progress bar
Local to Remote
Copy a file to a server:
scp file.txt user@192.168.1.10:/home/user/
You’ll be prompted for the password (unless you’ve set up SSH keys), and off it goes.
Want to rename the file during copy? Just specify the new name:
scp file.txt user@192.168.1.10:/home/user/newname.txt
If your server uses a non-standard SSH port:
scp -P 2222 file.txt user@192.168.1.10:/home/user/
For directories, add -r:
scp -r ./my-folder user@192.168.1.10:/home/user/
Remote to Local
Same idea, just flip the arguments:
scp user@192.168.1.10:/home/user/file.txt ./local-copy.txt
Between Two Remote Servers
This is where scp gets interesting. You can copy directly between two remote machines:
scp user1@host1.com:/files/data.txt user2@host2.com:/backup/
You’ll need to enter passwords for both servers. The data transfers directly between them—it doesn’t route through your local machine.
If you want the traffic to go through your machine (maybe the servers can’t talk directly), add -3:
scp -3 user1@host1.com:/files/data.txt user2@host2.com:/backup/
A Few Gotchas
Overwriting: scp will overwrite destination files without warning. No confirmation prompt.
Permissions: You need read access on the source and write access on the destination. Obvious, but easy to forget when you’re not root.
The colon matters: That : is how scp distinguishes local from remote paths. Forget it and you’ll end up with a local file named user@host instead of a remote copy.
For more complex syncing (especially if you’re doing incremental backups), rsync is usually the better choice. But for quick one-off file transfers, scp is hard to beat.