How to download a file from a server using the terminal
By Flavio
Published · Updated
Tagged: CLI
Learn how to download a file from a remote server with scp: the syntax, your SSH key, a custom port, folders, wildcards, uploads, and fixes for the common errors.
I’ve had the need to transfer a file I had on a server.
I was connected through SSH and I didn’t want to setup the SFTP connection (and I didn’t know if the SFTP connection was allowed), so I opened another terminal window and used the command scp.
Here’s the syntax:
scp -i ~/.ssh/yourkey user@example.com:/path/to/server.txt ./
Replace user@example.com, the remote path, and the SSH key path with your own values.
This will download the file in the same folder you are in, because I specified ./. You can change it to any folder on your computer, or give the file a new name:
scp -i ~/.ssh/yourkey user@example.com:/path/to/server.txt ~/Downloads/backup.txt
Run scp from your local terminal, not from the SSH session on the remote server. The remote server can’t see the files on your computer.
How scp knows which side is remote
The rule is simple: the first path is the source, the second is the destination, and anything with host: in front is on the server.
So user@example.com:/path/to/server.txt is a file on the server, and ./ is a folder on your computer.
A relative path after the colon starts from your home folder on the server, so user@example.com:notes.txt means /home/user/notes.txt for most users.
If a local file name contains a colon, like report:2026.txt, scp thinks report is a host. Write it as ./report:2026.txt so it’s clearly a local path.
Use a different port
If SSH on the server listens on a port other than 22, pass -P with a capital P:
scp -P 2222 -i ~/.ssh/yourkey user@example.com:/path/to/server.txt ./
This trips me up because ssh uses lowercase -p for the port. In scp, lowercase -p means something else: it preserves the file’s modification time and permissions.
Download a folder
You can copy a directory recursively with -r:
scp -r -i ~/.ssh/yourkey user@example.com:/path/to/folder ./
This creates folder inside your current directory. Note that scp follows symbolic links it finds inside the folder, so it copies the files they point to.
Download several files with a wildcard
To grab all the log files in a folder, put the remote path in quotes:
scp -i ~/.ssh/yourkey 'user@example.com:/var/log/app/*.log' ./
The quotes stop your local shell from trying to expand *.log on your computer. In zsh, the default shell on macOS, an unquoted pattern with no local match fails with no matches found before scp even runs.
Upload a file to the server
Swap the two paths to send a file the other way:
scp -i ~/.ssh/yourkey ./deploy.zip user@example.com:/var/www/
The user you connect as needs write permission on the destination folder. If you get Permission denied, upload to your home folder first, then move the file with sudo from an SSH session.
Skip the long options with ~/.ssh/config
If your key, user and port are already configured in ~/.ssh/config, you can omit them:
Host myserver
HostName example.com
User user
Port 2222
IdentityFile ~/.ssh/yourkey
Now the command is short:
scp myserver:/path/to/server.txt ./
Copy between two servers
You can also copy from one server to another, running the command on your computer:
scp server1:/path/to/file.txt server2:/path/to/
With current OpenSSH versions, the data goes through your computer by default, so your machine needs to be able to log in to both servers.
scp now uses the SFTP protocol
Since OpenSSH 9.0, scp uses the SFTP protocol under the hood instead of the old scp protocol. The commands stay the same, but a few edge cases changed:
- You don’t need double quoting for remote paths with spaces or special characters anymore. Old tricks like
"'my file.txt'"may now make the transfer fail. - Very old servers that don’t support SFTP, or that don’t expand paths starting with
~, may fail. Add-Oto use the legacy protocol:
scp -O user@example.com:~/server.txt ./
When it doesn’t work
Add -v to see what’s happening during the connection:
scp -v -i ~/.ssh/yourkey user@example.com:/path/to/server.txt ./
The most common errors:
Permission denied (publickey): the server didn’t accept your key. Check the-ipath, the user name, and that you can log in withssh -i ~/.ssh/yourkey user@example.com.No such file or directory: the remote path is wrong. Log in with ssh and runlson it to check.Connection refusedor a timeout: wrong port, or a firewall blocks it.
scp or rsync?
scp is great for a few files. For big folders, or transfers you might need to run again, I use rsync. It only sends what changed, and with --partial it keeps partially transferred files when the connection drops, so you can resume the download by running the same command again:
rsync -avz --partial -e "ssh -i ~/.ssh/yourkey" user@example.com:/path/to/folder/ ./folder/
For the full picture (keys, ~/.ssh/config, scp, and rsync), see SSH for developers.
Want me to talk about your product? You can sponsor this site.
Related posts about cli: