Tutorials  /  Linux Basics

Copy Directories in Linux with cp and rsync Commands

Ccentron Redaktion · August 2025 ·7 min read ·Linux Basics, Tutorial

A directory is a structure in a file system that stores files and other directories. It organizes data into a hierarchical format, helping users and operating systems manage information efficiently. Each directory can include files and subdirectories, forming a branching tree-like layout that can extend to multiple levels of data.

This guide outlines how to copy a directory in Linux using both the cp and rsync commands.

Prerequisites

  • Access to a Linux-based environment with a non-root sudo user account.
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Commands for Copying Directories in Linux

Below are the commands used to copy directories in Linux.

cp Command

The cp command copies files and directories in Linux. It is available in all Linux distributions and follows this syntax:

Code
cp [OPTIONS]... [SOURCE] [DESTINATION]
  • OPTIONS: Flags that adjust the command’s behavior.
  • SOURCE: The file or directory you intend to copy.
  • DESTINATION: The target directory path or filename for renamed copies. The command creates the destination directory if it does not exist.

cp Command Options

  • -r or -R: Recursively copy directories and their contents.
  • -a: Recursively copy while preserving symbolic links, permissions, ownership, and timestamps.
  • -v: Show the copying process in detail.
  • -p: Maintain file attributes such as mode, ownership, and timestamps.

rsync Command

The rsync command synchronizes files and directories between two locations, locally or remotely. It can copy data between local paths or between local and remote systems. Rsync uses timestamps and file sizes to identify changes, making it ideal for backups and large data transfers.

Local Copy

Code
rsync [OPTIONS]... [SOURCE_DIRECTORY]/ [DESTINATION_DIRECTORY]/

Local to Remote Copy

Code
rsync [OPTIONS]... [SOURCE_DIRECTORY]/ [USER@REMOTE_HOST]:[DESTINATION_DIRECTORY]/

Remote to Local Copy

Code
rsync [OPTIONS]... [USER@REMOTE_HOST]:[SOURCE_DIRECTORY]/ [DESTINATION_DIRECTORY]/

Syntax Explanation

  • OPTIONS: Flags that adjust behavior, e.g., -a (archive mode), -v (verbose), -r (recursive).
  • SOURCE_DIRECTORY/: The source directory. A trailing slash copies only its contents, not the directory itself.
  • DESTINATION_DIRECTORY/: The target directory for the copy, created automatically if absent.
  • USER@REMOTE_HOST: Defines the remote system (user, host, or IP) for remote transfers.

rsync Command Options

  • -a: Archive mode (recursive copy, symbolic links, permissions, timestamps, ownership preserved).
  • -v: Show detailed output during the copy.
  • -r: Recursively copy directories and their contents.
  • -z: Compress data during transfer to reduce bandwidth usage.

Copying a Directory with the cp Command

The following steps show how to create a directory structure, add files, and copy directories using cp with different options.

  1. Create a new directory in the current location:
Console
$ mkdir sample
  1. Move into the new directory:
Console
$ cd sample
  1. Create a file named file1.txt:
Console
$ touch file1.txt
  1. Add content to file1.txt:
Console
$ echo "This is a sample file in the main directory." > file1.txt
  1. Create a subdirectory named folder1 inside sample:
Console
$ mkdir folder1
  1. Create a file in folder1:
Console
$ touch folder1/file2.txt
  1. Add content to that file:
Console
$ echo "This file is inside folder1." > folder1/file2.txt
  1. Copy folder1 into folder1-backup:
Console
$ cp -r folder1 folder1-backup

The -r option copies all contents recursively.

  1. List all contents to confirm the copy:
Console
$ ls -R
  1. Go back to the parent directory:
Console
$ cd ..
  1. Copy the entire sample directory to sample-copy:
Console
$ cp -r sample sample-copy
  1. Copy with -v and -p flags:
Console
$ cp -rvp sample sample-copy-rvp
  1. Copy using the -a flag:
Console
$ cp -a sample sample-copy-a
  1. Verify the copied directories and their structure:
Console
$ ls -l sample sample-copy sample-copy-rvp sample-copy-a

The output shows that sample-copy has a newer timestamp because it was copied without the -p or -a flags. Copies made with -rvp and -a preserved timestamps and permissions.

Copying a Directory with Rsync

rsync is a versatile command-line tool for synchronizing files and directories. It is more advanced than cp and is particularly useful for backups, partial updates, and large-scale data transfers. Unlike cp, rsync copies only the differences between the source and destination, which reduces both time and bandwidth usage. Follow the steps below to copy directories with rsync.

rsync is pre-installed on most Linux systems. You can check if it’s available on your system with:

Console
$ rsync --version

If rsync is missing, refer to its installation instructions.

Ensure you are in the directory containing the sample directory.

View the structure of sample:

Console
$ ls -R sample

Copy the sample directory to a new directory using rsync:

Console
$ rsync -av sample/ sample-rsync/

This command copies the contents of sample to sample-rsync/ in archive mode, preserving file attributes and displaying detailed output.

Check the copied directory:

Console
$ ls -R sample-rsync

Add a new file to the sample directory:

Console
$ echo "This is a new file." > sample/extra.txt

This creates a file that is not yet present in sample-rsync.

Preview changes without applying them using the --dry-run flag, which simulates the copy process:

Console
$ rsync -av --dry-run sample/ sample-rsync/

Output:

Code
sending incremental file list
./
extra.txt

sent 254 bytes  received 24 bytes  556.00 bytes/sec
total size is 123  speedup is 0.44 (DRY RUN)

Note: extra.txt appears in the output because it is new. rsync detects and lists changes, transferring only what’s needed.

Update sample-rsync and display transfer progress using --progress:

Console
$ rsync -av --progress sample/ sample-rsync/

Output:

Code
sending incremental file list
./
extra.txt
            20 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=5/7)

sent 314 bytes  received 40 bytes  708.00 bytes/sec
total size is 123  speedup is 0.35

From this output, you can see only extra.txt was copied.

Verify the updated directory:

Console
$ ls -R sample-rsync

Remove a file from sample:

Console
$ rm sample/extra.txt

Use --delete to remove files from sample-rsync that no longer exist in sample:

Console
$ rsync -av --delete sample/ sample-rsync-copy/

Ensure both directories are synchronized:

Console
$ ls -l sample sample-rsync

Output:

YAML
sample:
total 12
-rw-rw-r-- 1 user user   45 May  7 06:06 file1.txt
drwxrwxr-x 2 user user 4096 May  7 06:06 folder1
drwxrwxr-x 2 user user 4096 May  7 06:07 folder1-backup

sample-rsync:
total 12
-rw-rw-r-- 1 user user   45 May  7 06:06 file1.txt
drwxrwxr-x 2 user user 4096 May  7 06:06 folder1
drwxrwxr-x 2 user user 4096 May  7 06:07 folder1-backup

You can see that extra.txt has been removed, while the rest of the directory structure remains intact.

Conclusion

You have learned how to copy directories in Linux using both the cp command and rsync. Each has advantages depending on the use case. For more details, run man cp for the cp manual page and man rsync for the rsync manual page.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren