← Back to blog

How to SCP a File: Upload, Download & scp -r Examples

·
SCP command examples to copy files and directories over SSH

Use scp ./file.txt user@host:/remote/path/ to upload a file, or scp user@host:/remote/file.txt ./ to download it. Add -r when the source is a directory.

SCP copies data over an SSH connection, so it uses your usual SSH username, host, port and authentication. The crucial detail is which computer runs the command: for a normal upload or download, open a terminal on your own computer rather than typing scp inside an existing remote SSH shell.

Quick answer — copy in either direction

# Upload: run on your local computer
scp ./report.txt [email protected]:/home/dev/uploads/

# Download: run on your local computer
scp [email protected]:/home/dev/uploads/report.txt ./

# Download an entire folder
scp -r [email protected]:/home/dev/project ./downloads/

The address 203.0.113.10, username dev and example paths are placeholders. Replace them with your real SSH account and server paths. Ensure the destination directory already exists. By default, SCP can overwrite a destination file with the same name without asking, so check your path before running it.

SCP syntax: source, destination, and which terminal to use

The command takes at least a source and a destination. A path prefixed with user@host: refers to the remote machine; a plain file path belongs to the machine running the command.

scp [options] source destination

# Remote path format:
user@host:/absolute/remote/path

# Local path examples:
./report.txt
/home/alex/Downloads/report.txt
TaskWhere to run SCPDirection
Upload a file to LinuxYour laptop, workstation or Windows PowerShellLocal → remote
Download a file from LinuxYour laptop, workstation or Windows PowerShellRemote → local
Copy between two serversTypically a third machine or local workstationRemote → remote

If you have already connected to the server with ssh dev@host, open a second local terminal to download files to your own computer. SCP starts a separate SSH-based transfer; it does not automatically send files back through your existing interactive shell. This is the recurring confusion in the Unix & Linux discussion about remote-to-local copies.

Prerequisites: you need the scp client locally, network access to the remote SSH port, an account accepted by SSH, read access to the source and write access to the destination. You do not normally need root or sudo.

Copy a local file to a remote server

To upload a report into an existing server directory, put the local file first and the server path second:

# Run locally in Linux, macOS or WSL
scp ./report.txt [email protected]:/home/dev/uploads/

The source is ./report.txt on your computer. The target is the uploads directory in the remote user's home. If the directory does not exist, create it first via a normal SSH command:

ssh [email protected] 'mkdir -p ~/uploads'
scp ./report.txt [email protected]:/home/dev/uploads/

To change the filename during upload, provide a complete target filename:

scp ./report.txt [email protected]:/home/dev/uploads/report-backup.txt

To copy to the remote account's home directory, you can use the shorthand user@host:. Remember that files with the same destination name may be replaced; consider uploading to a staging directory first for important deployments.

In our Windows lab, we first computed a local SHA-256. An initial attempt to use port 22224 was refused because Ubuntu was actually listening on port 22; confirm your server’s active port rather than blindly copying test values. The remote checksum for a successful Windows upload was not included in the logs we reviewed, so it is not counted as a verified transfer in the results table.

PowerShell SCP upload workflow and local SHA-256 verification
Lab capture 1. Windows to Ubuntu upload: PowerShell command and local SHA-256. An initial attempt to port 22224 failed; do not infer remote integrity without a separately checked server-side hash.
Verification, not guesswork

The transfer progress meter indicates activity, but it is not an end-to-end integrity check. For important files, compare local and remote SHA-256 values as described in the verification section.

Copy a file from a remote server to your local computer

Reverse the arguments: the remote file becomes the source and your local directory becomes the destination. Run this locally, not at the remote Linux prompt.

# Run on your own computer
scp [email protected]:/home/dev/uploads/report.txt ./

# Save with a different local name
scp [email protected]:/home/dev/uploads/report.txt ./downloaded-report.txt

The dot means your current local working directory. Check it with pwd on Linux/macOS, or Get-Location in PowerShell. If you want a specific destination folder, create that local folder before copying.

Windows PowerShell: download into Downloads

On Windows 10/11 with OpenSSH Client available, use scp.exe directly from PowerShell. You do not have to install SSH Server on your Windows computer just to pull files from Linux.

# Run in local Windows PowerShell (not Linux Bash)
Get-Command scp
Set-Location "$env:USERPROFILE\Downloads"
scp [email protected]:/home/dev/uploads/report.txt .
Get-Item .\report.txt

If you open PowerShell and first SSH into Linux, $env:USERPROFILE is not valid in that remote Bash shell. Exit the remote shell or open another local window. See our dedicated SCP for Windows guide for installation and Windows path handling.

Windows PowerShell SCP download with matched remote and local SHA-256
Lab capture 2. Verified remote-to-local copy: a 30-byte file downloaded with exit code 0, then matched SHA-256 on Windows and Ubuntu.

Download a whole remote folder

mkdir -p ./downloads
scp -r [email protected]:/home/dev/project ./downloads/

If downloads exists, the result is typically ./downloads/project/. For more details on directory shape, keep reading below.

Copy a directory with scp -r

The -r flag copies a directory recursively, including files and nested directories. Without it, SCP generally refuses to use a directory as a regular file source.

# Upload an entire local directory
scp -r ./project [email protected]:/home/dev/backups/

# Download an entire remote directory
scp -r [email protected]:/home/dev/project ./downloads/

Assuming the destination folders already exist, the first command creates or updates /home/dev/backups/project/; the second creates or updates ./downloads/project/. SCP does not recursively create missing parent directories just because -r is present.

Before a directory copy, you can inspect the source tree with find ./project -maxdepth 3 -print (Linux/macOS), or Get-ChildItem .\project -Recurse -Force (PowerShell). Afterward, inspect the destination tree and check that nested files arrived. See our more focused guide to copying directories with SCP.

SCP recursive folder copy showing hidden file, nested directories and symlink outcome
Lab capture 3. Verified recursive copy with exit code 0: nested files and a hidden .env.example arrived; the source symlink became a regular 19-byte file. README.md hashes matched.

Does a trailing slash change the result?

Do not assume SCP's trailing-slash behavior matches rsync. The meaning can depend on the client implementation and destination path. For an unambiguous result, specify the destination directory explicitly and inspect the resulting tree. The next section shows the reliable distinction between naming a folder and expanding a shell wildcard.

Directory versus contents: hidden files and symlinks

When you copy a named directory recursively, its hidden entries (names beginning with .) are part of that directory tree. But when you type ./project/* in a typical Bash shell, the shell expands the wildcard before SCP runs; hidden entries are normally excluded.

# Copies the folder as a directory, including hidden entries
scp -r ./project [email protected]:/home/dev/backups/

# Copies the visible items matched by the local shell
scp -r ./project/* [email protected]:/home/dev/visible-only/

For example, ./project/.hidden-marker will not normally be matched by * in Bash, but will be traversed when the project directory itself is copied with -r. If you need to reproduce a directory's contents exactly, including dotfiles, a tool such as rsync -a ./project/ ... is often clearer, provided rsync is installed on both ends.

SCP named directory compared with Bash wildcard contents copy
Lab capture 4. Two successful copies: full directory tree (4 directories, 6 files) included .env.example; Bash wildcard contents copy (3 directories, 5 files) omitted it.

What happens to symbolic links?

OpenSSH documents that scp -r follows symbolic links it encounters when walking the directory tree. A link can therefore cause data from outside your intended source folder to be copied. Inspect untrusted source trees before recursively transferring them; when link preservation matters, consider a suitable archive or a synchronization tool with explicit symlink behavior.

Security tip: avoid demonstrating wildcard behavior using real .env files, private keys or production credentials. Use an inert file such as .hidden-marker in a temporary laboratory directory.

SCP options: custom port, SSH key, multiple files and spaces

FlagWhat it doesCommon mistake
-P 2222Connect to a non-default SSH portUse uppercase P; lowercase p is different
-i keyfileChoose an SSH identity/private keyThe key must be a valid, permitted SSH identity
-pPreserve modification/access times and file mode bitsDoes not preserve ownership
-rCopy a directory recursivelyDoes not mean resume
-vShow SSH/SCP connection diagnosticsDebug output may reveal hostnames and paths
-CRequest SSH compressionMay not help already compressed content

Use a custom SSH port and private key

scp -P 2222 -i ~/.ssh/id_ed25519 \
  ./report.txt [email protected]:/home/dev/uploads/

Unlike ssh -p 2222, SCP spells the port flag as uppercase -P. The lowercase -p preserves source timestamps and mode bits. If you use an SSH config alias, SCP also reads settings such as Port, User and IdentityFile from ~/.ssh/config.

Copy several files in one command

scp ./report.txt ./notes.txt ./summary.csv \
  [email protected]:/home/dev/uploads/

# Bash: local glob expansion
scp ./*.log [email protected]:/home/dev/uploads/

The last argument is the destination. Local * patterns are expanded by your shell, so their behavior can differ between Bash and PowerShell. Remote glob patterns also have compatibility differences between modern SFTP-backed SCP and legacy SCP.

Copy paths containing spaces

scp './quarterly report.txt' \
  '[email protected]:/home/dev/Shared Files/'

scp '[email protected]:/home/dev/Shared Files/report.txt' ./

These quotes protect the argument from the local shell. Modern OpenSSH SCP uses SFTP by default, so older advice involving several layers of remote shell escaping may be unnecessary or wrong. Legacy -O transfers have different quoting rules.

Copy files between two remote servers

Current OpenSSH SCP normally moves data between two remote hosts through the machine running SCP. That means your local machine needs working SSH credentials for both hosts.

# Run on your local workstation; data passes through it
scp [email protected]:/home/dev/report.txt \
  [email protected]:/home/ops/incoming/

For a direct remote-to-remote transfer, -R connects to the source host and runs SCP there. That source host must then be able to authenticate to the destination without an interactive password prompt:

scp -R [email protected]:/home/dev/report.txt \
  [email protected]:/home/ops/incoming/

Do not enable SSH agent forwarding blindly just to make -R work: forwarding carries security implications. For everyday transfers, use the default route or an already-secured host-to-host configuration. The OpenSSH manual documents both modes.

Verify that SCP actually copied the right file

Check both the process exit status and the destination. An exit code of 0 means the program reports success, but for high-value data you can compare content hashes as an independent check.

# Bash / Linux / macOS / WSL (immediately after scp)
echo $?

# Windows PowerShell (immediately after scp)
$LASTEXITCODE
# After an upload, run locally on Linux/macOS/WSL
sha256sum ./report.txt

# Query the uploaded copy on the remote Linux server
ssh [email protected] 'sha256sum /home/dev/uploads/report.txt'

Compare the 64-character SHA-256 digest at the beginning of each line. Matching hashes are strong evidence that the two file contents are identical. Do not compare the filenames at the end of the output—they may legitimately differ if you renamed the file during transfer.

Equivalent Windows check

# Local Windows PowerShell
(Get-FileHash .\report.txt -Algorithm SHA256).Hash

# Remote Linux server, via local PowerShell
ssh [email protected] 'sha256sum /home/dev/uploads/report.txt'

PowerShell prints hexadecimal letters in uppercase while sha256sum usually prints lowercase; compare digests case-insensitively. If you need to prove a whole directory copied correctly, compare an inventory of relative paths and their hashes, not just one representative file.

Common SCP errors and what to check first

Most failed transfers trace back to the wrong terminal, an incorrect path, authentication, insufficient permissions, or a service that cannot accept the connection. Diagnose those separately instead of immediately changing SSH server settings.

Message or symptomLikely meaningFirst safe check
Permission denied (publickey)SSH authentication failedTry ssh -v user@host with the same identity
Permission denied for destination pathAccount lacks destination write permissionssh user@host 'ls -ld /target/dir'
No such file or directorySource or destination parent path is wrong/missingUse ls or ssh ... ls on the relevant machine
not a regular file / is a directoryA directory was specified without -rCheck source type; retry with scp -r
Connection refusedNo reachable listener on that host/portCheck host, -P port and SSH service/socket
subsystem request failedSFTP subsystem unavailable or deniedInspect server configuration/logs; legacy fallback only if appropriate

Read the error on the correct machine

# Local machine: does this file exist?
ls -l ./report.txt

# Remote Linux server: does the destination directory exist?
ssh [email protected] 'ls -ld /home/dev/uploads'

# Client debug output (redact sensitive paths before sharing)
scp -v ./report.txt [email protected]:/home/dev/uploads/

The wording of an error can differ across OpenSSH versions and underlying protocols. For a folder error, see SCP “not a regular file” troubleshooting. For network-level refusals, see SSH connection refused.

Real SCP error outputs for missing recursive flag and nonexistent source path
Lab capture 5. Controlled failures: directory without -r returned “not a regular file” (exit 1); missing local file returned “No such file or directory” (exit 255).

Modern SCP uses the SFTP protocol by default

The SCP command-line tool is not always using the old SCP wire protocol. Since OpenSSH 9.0 (April 2022), the scp utility uses SFTP for transfers by default. The familiar commands remain the same.

# Inspect your client's OpenSSH version
ssh -V

# Debug a small test transfer and examine the protocol negotiation
scp -v ./report.txt [email protected]:/home/dev/uploads/

In our Ubuntu 24.04 LTS test, scp -v printed Executing: program /usr/bin/ssh host 127.0.0.1, user yegor_sigma, command sftp. This proves the client selected the SFTP subsystem. The later verification for that particular run found the destination file missing, so we do not describe it as a successfully completed transfer. For an older server without SFTP support, OpenSSH provides -O to request the original SCP/RCP protocol:

# Compatibility only: legacy SCP protocol
scp -O ./report.txt [email protected]:/home/dev/uploads/

The legacy protocol involves the remote shell and has different wildcard and quoting behavior. Do not add -O as a routine fix for unrelated network, authorization or filesystem errors. See the SCP vs SFTP comparison and OpenSSH 9.0 release notes.

OpenSSH SCP verbose log demonstrating SFTP subsystem selection
Lab capture 6. OpenSSH 9.6p1 verbose output selected “command sftp”. This log establishes protocol selection only: completion of that particular transfer was not verified.

When to use SFTP, rsync or a file browser instead

SCP is useful for a quick one-shot copy. It is less convenient if you need to explore folders interactively, resume a failed transfer or repeatedly synchronize a large tree.

NeedBetter fitWhy
Upload or download a known filescpOne command, easy to script
Browse and download interactivelysftp / ShadeTerm SFTP browserNavigate before transferring
Repeated directory synchronizationrsyncCompares existing files and supports more efficient retries
Preserve a tree of symlinks and metadataConfigured archive or synchronization workflowMore explicit control than default scp -r traversal
# Start an interactive OpenSSH SFTP session locally
sftp [email protected]

After the sftp> prompt appears, enter:

get /home/dev/uploads/report.txt
bye

For repeated synchronization, use rsync instead, if it is installed on both systems:

rsync -a ./project/ [email protected]:/home/dev/project/

ShadeTerm's server workspace includes an SFTP file browser alongside terminals and a code editor. It is useful when the remote file path is not known in advance, but command-line SCP remains a good choice for automation and reproducible copy commands.

Hands-on SCP tests: original lab results

These are original terminal captures from the SSHFlow-era lab, retained as evidence after the ShadeTerm rebrand; they were not rerun on the migration date.

We tested the commands rather than relying on example-only output. Our lab used Windows PowerShell with OpenSSH as a client, plus Ubuntu 24.04 LTS in WSL2 with OpenSSH_9.6p1 (Ubuntu-3ubuntu13.20) as the SSH server. The Linux-to-Linux tests connected over SSH to 127.0.0.1:22 within WSL, so they exercise a real SSH transfer but do not measure Internet latency or cross-machine throughput. All files were disposable examples, not production data.

TestWhat we actually observedVerification
Windows → Ubuntu uploadA Windows source SHA-256 was recorded. An initial upload targeted the wrong port (22224) and failed with Connection refused. A successful retry and matching remote hash were not included in the supplied terminal logs.Not independently confirmed from logs
Ubuntu → Windows downloadscp exited 0; a 30-byte file was downloaded and its contents checked with Get-Content.PASS — Windows and Linux SHA-256 matched
Recursive scp -r copyscp exited 0; nested directories and .env.example arrived. A source symlink named readme-link became a regular 19-byte file on the destination.PASS — matching README SHA-256
Directory vs contentsBoth transfers exited 0. The full folder copy contained .env.example; the contents copied using Bash * did not.PASS — two actual trees and explicit hidden-file checks
Intentional errorsCopying a directory without -r returned not a regular file (exit 1); a nonexistent local path returned No such file or directory (exit 255).PASS — error paths reproduced
Default SFTP protocolscp -v on OpenSSH 9.6p1 logged Executing: ... command sftp. A subsequent file check did not find the destination in that test attempt.Protocol selected; transfer completion not proven for that attempt

Evidence standard. We mark an operation successful only when we have the command result and an independent destination check. A successful SSH handshake or a verbose sftp line alone does not prove that a file was copied. Exit codes here are observations from this OpenSSH version, not guaranteed error-code mappings across implementations.

For the successful Windows download, the SHA-256 on both systems was 6330b21bf80e7ef8e7db9539baf5024d52134abd8f972ad556281b2d851fc43e. For the recursive test, both copies of README.md produced 6cfb26dd0e955356f407c3acc7be5778fdb3358c0fa8b2f7be6f85a7dad9f75c. Case differences in the hexadecimal output are cosmetic.

Limitations: these tests do not establish SCP performance, behavior on other SSH server implementations, all shell glob rules, or preservation of metadata. The SSH/SFTP defaults are documented separately by OpenSSH in the sources below. Screenshots show the lab command output; the exact files and paths are purpose-built test data.

Frequently asked questions

How do I SCP a file from a server to my computer?

Open a terminal on your computer and run scp user@host:/remote/path/file.txt ./. The period is your local current directory. If you're already inside an SSH session on the server, open a separate local terminal first.

How do I copy an entire folder with SCP?

Add -r: scp -r user@host:/remote/project ./downloads/. The destination parent directory must exist. To copy a local directory up to the server, reverse the two paths.

Does scp -r copy hidden files?

Copying a named directory recursively includes its hidden entries. But scp -r ./project/* ... relies on local shell expansion and typically omits dotfiles in Bash. This difference is one reason to inspect the destination after a directory copy.

What is the difference between scp -P and scp -p?

Uppercase -P sets the SSH port, as in -P 2222. Lowercase -p preserves source file timestamps and mode bits.

Can I use SCP on Windows?

Yes. With Microsoft's OpenSSH Client installed, run scp in Windows PowerShell or Windows Terminal. For example, scp user@host:/remote/file.txt . downloads into the current Windows directory.

Does SCP use SFTP or the original SCP protocol?

OpenSSH 9.0 and later use SFTP by default for the scp command. The -O flag selects the legacy protocol when compatibility genuinely requires it.

Can SCP resume an interrupted file transfer?

The basic scp workflow does not provide a resume option comparable to synchronization or resume-capable tools. For large interrupted transfers, consider rsync or an appropriate SFTP client.

Primary references and further reading

Command semantics and protocol behavior are sourced primarily from OpenSSH documentation. The Stack Exchange examples are included to clarify common user mistakes, not as substitutes for the upstream manual. We will keep independently measured results separate from all published references.

Bottom line: SCP runs from the computer doing the copying

Use scp ./file user@host:/remote/path/ to upload and scp user@host:/remote/file ./ to download. Add -r for directories, uppercase -P for a custom port and -i for a selected SSH key. Run the command from your local terminal, confirm the destination exists and verify important data with SHA-256.

For more focused help, see directory copying, SCP on Windows, SCP vs SFTP, and interactive SFTP commands.

Why trust us
120+Published guides
30+Topics covered
View latest guides