SCP for directory: directory file transfers in Linux

Summarize with:

What is SCP for directory?

SCP (Secure Copy) can transfer a whole directory, including its files and subdirectories. SCP supports recursive copying, which allows you to transfer an entire directory structure from a source location to a destination location.

To transfer a directory using SCP, you can use the “-r” flag, which stands for recursive. The recursive flag tells SCP to copy the directory and all its contents, including subdirectories and files. Here’s an example of how to use SCP to transfer a directory:

scp -r [source_directory] [username]@[destination_host]:[destination_directory]

When you run the command, SCP will recursively copy the entire directory and its contents to the specified destination directory on the remote server. You may be prompted to enter the password for the destination server’s username, or if you have SSH key-based authentication set up, you may not need to enter a password.

SCP command syntax for directories

To use the SCP (Secure Copy) command to transfer an entire directory, you can follow these steps:

  • Open a terminal or command prompt on your local machine and navigate to the directory where you want to copy the directory from.
  • Use the following SCP command syntax:
scp -r [source_directory] [username]@[destination_host]:[destination_directory]
  • Replace [source_directory] with the path to the directory you want to copy. Specify [username] as the username on the destination server, [destination_host] as the IP address or hostname of the destination server, and [destination_directory] as the path where you want to copy the directory to.
  • Enter the password for the destination server’s username when prompted. If you have SSH key-based authentication set up, you may not need to enter a password.
  • The SCP command will start copying the entire directory and its contents recursively to the destination server. You’ll see the progress and file transfer details in the terminal.

Once the SCP command completes, the entire directory and its contents will be transferred to the specified destination directory on the remote server.

Note: Ensure that you have proper permissions and access rights on both the source and destination directories to perform the SCP transfer. Otherwise, you can have a SCP permission denied error.

Useful SCP flags

Beyond -r for recursive copying, a few flags make SCP more practical for everyday transfers:

  • -P [port] — connects to a custom SSH port. This trips up many users: the port flag in SCP is an uppercase -P (lowercase -p does something else). Use it whenever the server’s SSH isn’t on the default port 22.
  • -p — preserves the original modification times, access times, and permissions of the files.
  • -C — compresses the data during transfer, which can speed things up over slower connections.
  • -v — verbose mode, printing debug output. Useful when a transfer fails and you need to see where it breaks.

You can combine them. For example, to copy a directory over a custom port (2222), preserving attributes and showing progress:

scp -rpv -P 2222 ./mysite user@203.0.113.10:/var/www/Code language: JavaScript (javascript)

Uploading and downloading directories

The direction of the copy depends simply on the order of the arguments — local-to-remote uploads, remote-to-local downloads.

Upload a local directory to the server:

scp -r ./mysite user@203.0.113.10:/var/www/Code language: JavaScript (javascript)

Download a directory from the server to your local machine (just swap the order — remote first, local second):

scp -r user@203.0.113.10:/var/www/mysite ./Code language: JavaScript (javascript)

The trailing ./ means “the current local folder.” You can replace it with any local path, such as /home/user/backups/.

Alternatives to SCP for directory

These alternatives provide flexibility and additional features for copying directories, allowing you to choose the method that best suits your requirements and preferences.

ToolBest forSpeed on large transfersResumes if interrupted?Ease of use
SCP
Quick, one-off directory copiesGoodNoVery easy — one command
rsync
Recurring syncs, backups, large dirsExcellent (delta transfer)YesEasy, more flags to learn
tar + SSH
Many small files at onceVery good (single archive)NoModerate — multi-step
SFTP
Interactive browsing & transfersGoodYesEasy — interactive session

Rsync to copy an entire directory

Rsync is the most popular alternative to SCP for directories, and often the better choice. Its key advantage is delta transfer: it compares source and destination and copies only what changed, instead of resending everything. That makes it much faster for repeated transfers, large directories, or syncing backups.

It also preserves permissions and timestamps, compresses data in transit, and can resume interrupted transfers — picking up where it stopped instead of starting over. For ongoing syncs or unreliable connections, rsync is hard to beat.

Here’s an example of using rsync to copy a directory:

rsync -av [source_directory] [destination_host]:[destination_directory]Code language: CSS (css)

The -a flag (archive) preserves permissions, timestamps, and structure; -v (verbose) shows the progress. Add -z to compress during transfer, or --progress for a detailed view.

tar + SSH

When transferring many small files, bundling them into a single compressed archive first is often faster than copying them one by one. The tar command packs a whole directory into one file (a “tarball”), preserving its structure and permissions, and you pipe it over SSH to the destination.

This approach shines on directories with thousands of small files, where the per-file overhead of SCP or SFTP slows things down.

# Creating the archive
tar -czf archive.tar.gz [source_directory]

# Copying the archive via SSH
scp archive.tar.gz [username]@[destination_host]:[destination_directory]

# Extracting the archive on the destination
ssh [username]@[destination_host] "tar -xzf [destination_directory]/archive.tar.gz -C [destination_directory]"
Code language: PHP (php)

In the tar flags, -c creates, -z compresses with gzip, and -f sets the filename; on extraction, -x extracts and -C sets the target folder.

SFTP

If you have SSH access to the remote server, you can use SFTP (SSH File Transfer Protocol) to copy directories interactively via port 22. It’s similar to FTP, but with a security layer. SFTP provides a more user-friendly interface compared to SCP. Here’s an example of using SFTP to copy a directory:

sftp [username]@[destination_host]
# Enter password if prompted

sftp> put -r [source_directory] [destination_directory]Code language: CSS (css)

Using SFTP, you can navigate the local and remote directories, transfer files and directories, and manage permissions and attributes.

The difference between SCP and SSH

The two are related but not the same: SSH is the secure channel, and SCP is a tool that travels through it. SSH (Secure Shell) provides the encrypted connection between two machines, used for remote login and as the transport layer for other tools. SCP (Secure Copy) is built on top of SSH specifically to copy files and directories over that connection.

In practice, it works like this: when you run an SCP command, it opens an SSH connection to the server and authenticates you (by password or SSH key). Once the secure channel is established, SCP transfers your files through it — encrypting the data in transit and verifying its integrity on arrival, so the files can’t be read or tampered with along the way.

In short, SSH is how the secure connection happens; SCP is what uses that connection to move your files.

Is SCP still safe to use?

Worth knowing: the SCP protocol is considered legacy. Because of security weaknesses that are hard to fix without breaking compatibility (such as CVE-2020-15778), the OpenSSH developers deprecated the old SCP protocol in OpenSSH 9.0 (April 2022).

In practice, this doesn’t mean the scp command stopped working. Since OpenSSH 9.0, the scp command still exists, but it uses the SFTP protocol under the hood by default instead of the old SCP protocol. For everyday transfers, you can keep using scp exactly as shown in this guide.

If you specifically need the old protocol — for example, to connect to a very old server — you can force it with the uppercase -O flag. But for new workflows, the recommendation is to prefer SFTP or rsync, both of which are more modern and actively maintained.

Performance while transferring a whole directory using SCP

For a single, one-off copy, SCP and rsync perform similarly — SCP is simple and does the job. The difference shows up on repeated transfers and large directories, where rsync is generally faster for three reasons:

  • Delta transfer: rsync compares source and destination and copies only what changed, instead of resending the whole directory every time. On a folder you sync regularly, this is a dramatic difference.
  • Compression: rsync can compress data in transit (the -z flag), reducing transfer time over slower connections.
  • Resumable transfers: if the connection drops, rsync picks up where it left off rather than starting over — valuable on large transfers or unstable networks.

So the choice comes down to the task: use SCP for quick, one-time copies where simplicity matters, and rsync for recurring syncs, backups, or large directories where efficiency pays off. And as noted above, since the SCP protocol is now legacy, both rsync and SFTP are the recommended choices for new workflows.

Need a server with full SSH access? SCP, rsync, and SFTP all need a server you fully control. Copahost’s Cloud and VPS plans give you root SSH access, NVMe storage, and our own data center — ready for your transfers and deployments.
Root SSH access NVMe storage Own data center
View VPS & Cloud plans

Frequently asked questions

How do I copy an entire folder with SCP?
Use the -r (recursive) flag, which copies the directory and everything inside it: scp -r ./mysite user@host:/var/www/. Without -r, SCP only copies single files, not folders.
What does scp -r do?
The -r flag stands for recursive. It tells SCP to copy a directory along with all its subdirectories and files, preserving the folder structure at the destination.
How do I download a directory from a remote server with SCP?
Just reverse the order of the arguments — put the remote path first and the local path second: scp -r user@host:/var/www/mysite ./. The trailing ./ means the current local folder.
How do I use SCP with a different port?
Use the uppercase -P flag followed by the port number: scp -P 2222 -r ./mysite user@host:/var/www/. Note it’s an uppercase -P in SCP (lowercase -p preserves timestamps instead) — a common source of errors.
SCP vs rsync: which one should I use?
Use SCP for quick, one-time copies where simplicity matters. Use rsync for recurring syncs, backups, or large directories — its delta transfer only copies what changed, making it faster and resumable.
Is SCP deprecated or still safe to use?
The legacy SCP protocol was deprecated in OpenSSH 9.0 (2022), but the scp command still works — it now uses the SFTP protocol under the hood by default. You can keep using it for everyday transfers, though SFTP and rsync are the recommended choices for new workflows.
Share the Post:
Picture of Gustavo Gallas

Gustavo Gallas

Graduated in Computing at PUC-Rio, Brazil. Specialized in IT, networking, systems administration and human and organizational development​. Also have brewing skills.