[SOLVED] How to Copy a Directory (and its contents) in Linux
Copying directories and their entire contents is a fundamental and frequent task for any Linux user, from system administrators managing complex server backups to developers organizing project files. Whether you’re creating backups, migrating data, or restructuring your filesystem, knowing the right commands is crucial.
This guide provides a clear, step-by-step approach to copying directories in Linux using the two most effective and widely-used commands: cp
and rsync
. We’ll cover the essential options and explain when to use each for optimal results.

Here are the steps to copy a directory in Linux:
Method 1: Using the cp
Command for Basic Copying
The most straightforward method for copying a directory is the cp
(copy) command. By using the recursive option, you can easily duplicate a directory and everything within it.
What is the cp -r
Command?
The -r
or -R
flag, short for “recursive,” tells the cp
command to copy not just the specified directory but also all the files and subdirectories contained within it, preserving the folder structure.
Step 1 – Open your terminal.
- Open your terminal, either connect to the server via SSH, via the console, or bash shell
Step 2 – Use the cp -r command to copy a directory
- Type the following command and press Enter
cp -r /path/to/source/directory /path/to/destination/directory
Replace /path/to/source/directory
with the path to the directory you want to copy and /path/to/destination/directory
with the path to the location where you want to copy the directory.
Press Enter.
The cp command will copy the directory and all of its contents to the destination directory. This command is used for copying files and directories. If the destination directory does not exist, it will be created. The destination folder is the location where the directory and its contents will be copied.
Article Updater for TurboGeek
Custom Gem
Show thinking
Part 1: Full-Text Rewrite for SEO & Accuracy
How to Copy a Directory in Linux: A Practical Guide (2025)
Copying directories and their entire contents is a fundamental and frequent task for any Linux user, from system administrators managing complex server backups to developers organizing project files. Whether you’re creating backups, migrating data, or restructuring your filesystem, knowing the right commands is crucial.
This guide provides a clear, step-by-step approach to copying directories in Linux using the two most effective and widely-used commands: cp
and rsync
. We’ll cover the essential options and explain when to use each for optimal results.
Method 1: Using the cp
Command for Basic Copying
The most straightforward method for copying a directory is the cp
(copy) command. By using the recursive option, you can easily duplicate a directory and everything within it.
What is the cp -r
Command?
The -r
or -R
flag, short for “recursive,” tells the cp
command to copy not just the specified directory but also all the files and subdirectories contained within it, preserving the folder structure.
How to Use cp -r
- Open Your Terminal: Access your system’s command line, either directly or via an SSH connection.
- Execute the Command: Use the following syntax to perform the copy operation:Bash
cp -r /path/to/source_directory /path/to/destination_directory
source_directory
: The absolute or relative path to the directory you want to copy.destination_directory
: The path where the copy will be placed.
Example:
To copy a directory named project-files
from your home directory to a backup folder, you would use:
Bash
cp -r ~/project-files /mnt/backups/
- If
/mnt/backups/
exists, theproject-files
directory will be created inside it. - If
/mnt/backups/
does not exist,cp
will attempt to create it.
Recommended cp
Options for Better Copying
While -r
is essential, combining it with other options ensures a more reliable and informative copy process. The -a
(archive) option is often considered best practice.
The -a
flag is equivalent to using -dR --preserve=all
and provides several key benefits:
-d
: Preserves symbolic links as symbolic links, rather than copying the files they point to.--preserve=all
: Preserves all file attributes, including ownership (user and group), timestamps (modification and access times), and permissions.
For a robust copy that maintains file integrity, use the -av
options:
cp -av /path/to/source_directory /path/to/destination_directory
The -v
(verbose) flag provides detailed output, showing you each file as it’s being copied, which is useful for monitoring progress.
Method 2: Using rsync
for Advanced and Remote Copying
For more advanced needs, especially when dealing with large directories, remote systems, or the need for synchronization, rsync
is the superior tool. It’s a powerful utility designed for efficient and resilient file transfers.
Why Choose rsync
?
- Efficiency:
rsync
uses a delta-transfer algorithm, which means it only copies the differences between the source and destination. This makes subsequent backups incredibly fast. - Resilience: It can resume interrupted transfers.
- Flexibility: Easily copies files to or from a remote server over SSH.
- Advanced Control: Offers fine-grained control over what to include or exclude.
Here are the steps to copy a directory using rsync:
Step 1 – Open your terminal.
- Open your terminal, either connect to the server via SSH, via the console, or bash shell
Step 2 – Use the rsync command to copy a directory including hidden files
- Type the following command and press Enter:
rsync -av /path/to/source/directory/ /path/to/destination/directory/
Replace /path/to/source/directory/
with the path to the directory you want to copy and /path/to/destination/directory/
with the path to the location where you want to copy the directory.Note the use of the trailing slashes after the directory paths. This tells rsync
Copy the source directory’s contents to the destination directory, rather than creating a new directory within the destination directory.
Press Enter.
The rsync command will copy the directory and all of its contents to the destination directory. This copying process is efficient and reliable. Unlike the cp command, rsync also provides options for incremental copying, partial transfers, and resuming interrupted transfers, which can be useful for large or complex directory structures. To copy multiple directories using rsync, you can specify each source directory separated by a space, followed by the destination directory.
The Importance of the Trailing Slash
The use of a trailing slash (/
) on the source directory is critical and changes the behavior of rsync
:
source_directory/
(with slash): Copies the contents of the source directory into the destination.source_directory
(without slash): Copies the source directory itself into the destination.
Example Scenario:
Let’s say we have a directory /home/user/data
.
- Command:
rsync -a /home/user/data/ /mnt/backups/
- Result: The contents of
data
(all its files and subfolders) are placed directly inside/mnt/backups/
.
- Result: The contents of
- Command:
rsync -a /home/user/data /mnt/backups/
- Result: A new directory named
data
is created inside/mnt/backups/
, resulting in/mnt/backups/data
.
- Result: A new directory named
Useful rsync
Options
To get the most out of rsync
, combine the -a
flag with other useful options:
rsync -avhP /path/to/source_directory/ /path/to/destination_directory/
-a
(archive): Same as withcp
, preserves permissions, ownership, and timestamps.-v
(verbose): Shows detailed output.-h
(human-readable): Displays file sizes in a human-friendly format (e.g., KB, MB, GB).-P
: Combines the flags--progress
(shows transfer progress per file) and--partial
(keeps partially transferred files for easier resuming).
This combination provides a robust, informative, and efficient way to copy directories, whether on a local machine or between remote systems.
Want to learn more Linux facts? Check out the rest of our Tech Quicky content!!
Thanks for taking the time to read this article. if you have any questions or feedback, please write in the comment section below.
Recent Comments