How To Make A Copy Of A Directory In Linux : Copy Linux Directory Recursively

Copying an entire directory in Linux uses the cp command with the -r flag for recursive copying. If you’re new to Linux or just need a refresher on how to make a copy of a directory in linux, this guide walks you through every step clearly. You’ll learn the essential commands, common options, and practical examples to handle directory copies like a pro.

Whether you’re backing up files, duplicating project folders, or moving data between locations, mastering directory copying saves time and prevents data loss. Let’s start with the basics and build up to advanced techniques.

How To Make A Copy Of A Directory In Linux

The core command for copying directories is cp -r. The -r flag stands for recursive, meaning it copies the directory and all its contents—subdirectories, files, and hidden items—into a new location. Without -r, cp only works on files, not directories.

Here’s the basic syntax:

cp -r source_directory destination_directory

For example, to copy a folder named “Projects” into a folder called “Backups”:

cp -r Projects Backups/

This creates a copy of “Projects” inside “Backups” with the same name. If “Backups” doesn’t exist, it creates a new directory with that name and copies the content into it.

Essential Options For Directory Copying

The cp command has several flags that modify its behavior. Knowing these helps you avoid common mistakes like overwriting files or missing hidden data.

  • -r or -R: Recursive copy (required for directories)
  • -a: Archive mode; preserves permissions, ownership, timestamps, and copies recursively. Equivalent to -dR --preserve=all
  • -v: Verbose; shows each file as it’s copied
  • -i: Interactive; prompts before overwriting files
  • -u: Update; copies only when the source is newer or the destination is missing
  • -n: No-clobber; prevents overwriting existing files
  • -p: Preserves file attributes (mode, ownership, timestamps)

Using -a is often better than -r because it keeps file metadata intact. For most backups, this is what you want.

cp -a source_directory destination_directory

Copying A Directory To A Different Location

To copy a directory to another location on the same filesystem or a different one, specify the full or relative path for both source and destination.

  1. Open a terminal.
  2. Navigate to the parent directory of the source if needed, or use absolute paths.
  3. Run the command: cp -r /home/user/Documents /mnt/backup/
  4. Verify with ls -l /mnt/backup/

If the destination path doesn’t exist, cp creates it. But if the destination is an existing directory, the source folder is placed inside it. To copy only the contents (not the folder itself), add a trailing slash to the source path.

cp -r /home/user/Documents/ /mnt/backup/

This copies all files and subdirectories from “Documents” directly into “backup”, without creating a “Documents” subfolder.

Preserving File Permissions And Timestamps

When copying directories for backup or migration, preserving permissions and timestamps is crucial. Use the -p flag or better yet, -a.

cp -a original_dir backup_dir

The -a flag combines recursive copying with preservation of all attributes. It also handles symbolic links correctly by copying the link itself rather than the target file. This is ideal for system backups or when moving data between servers.

If you only need to preserve specific attributes, use --preserve=mode,ownership,timestamps or combine flags like -rp.

Copying Hidden Files And Directories

Hidden files (those starting with a dot) are included automatically when you use -r or -a. However, if you use a wildcard like * to select files, hidden files are excluded by default.

To copy all files including hidden ones, avoid wildcards and specify the directory directly:

cp -r /home/user/MyFolder /home/user/MyFolder_Backup

If you must use wildcards, include the dot prefix:

cp -r /home/user/MyFolder/. /home/user/MyFolder_Backup/

This copies everything inside “MyFolder”, including hidden items, into “MyFolder_Backup”.

Copying Directories With Rsync (Advanced)

While cp works for simple copies, rsync is more powerful for large or repeated transfers. It can copy locally or over a network, and it only transfers differences, saving time.

Basic rsync command for directory copy:

rsync -av source_directory/ destination_directory/

The -a flag preserves attributes, and -v shows progress. The trailing slashes matter: a slash on the source means copy the contents, not the folder itself. A slash on the destination ensures the target is treated as a directory.

To copy a directory recursively with rsync:

rsync -av /home/user/Projects/ /mnt/backup/Projects/

Rsync is especially useful for incremental backups. Run the same command again later, and it only copies new or changed files.

Common Mistakes And How To Avoid Them

Even experienced users make errors when copying directories. Here are pitfalls to watch for:

  • Forgetting the -r flag: Without it, cp returns an error like “omitting directory”. Always include -r or -a.
  • Overwriting files unintentionally: Use -i or -n to prevent accidental overwrites.
  • Copying into a non-existent path: If the destination path doesn’t exist, cp creates it. But if you mistype the path, you might copy to the wrong place. Double-check paths.
  • Missing hidden files: Using wildcards like * excludes dot files. Copy the directory directly instead.
  • Permission errors: If you lack read permission on the source or write permission on the destination, the copy fails. Use sudo if needed, but be cautious.

Copying Directories With Sudo

When copying system directories or files owned by root, you need superuser privileges. Prepend sudo to the command:

sudo cp -r /etc/ /home/user/backup_etc/

This copies the entire /etc directory to your home folder. Be careful with sudo—it can overwrite important system files if you’re not precise.

Using Wildcards With Directory Copies

Wildcards like * and ? can select multiple directories. For example, to copy all directories starting with “log”:

cp -r log* /backup/

This copies “log1”, “log_data”, etc., into /backup. Wildcards work with relative and absolute paths. Remember that hidden directories are not matched by *.

Copying Directories Between Different Filesystems

Copying across filesystems (e.g., from ext4 to NTFS or to a network mount) works the same way, but you may lose some attributes like permissions or extended attributes. Use -a to preserve as much as possible, but the destination filesystem’s limitations apply.

For network transfers, consider using rsync over SSH for security and efficiency:

rsync -avz -e ssh /local/dir/ user@remote:/remote/dir/

The -z flag compresses data during transfer, speeding up large copies over slow connections.

Checking Copy Progress And Verifying

To see what’s being copied, use the -v flag. For large directories, you might want a progress bar. rsync with --progress shows per-file progress:

rsync -av --progress source/ destination/

After copying, verify the integrity with diff or rsync -c (checksum comparison):

diff -r source_directory destination_directory

If no output appears, the directories are identical. For large datasets, rsync -avc compares checksums but takes longer.

Copying Directories With Cpio Or Tar

Sometimes you need to copy directories while preserving hard links or special files. The cpio and tar commands can handle these cases.

Using tar to copy a directory:

tar cf - source_directory | (cd destination_parent && tar xf -)

This streams the directory through tar, extracting it at the destination. It preserves permissions, ownership, and hard links.

Using cpio:

find source_directory -depth -print | cpio -pdm destination_parent

The -p flag puts files into a directory, -d creates directories as needed, and -m preserves modification times.

Automating Directory Copies With Scripts

For regular backups, write a simple shell script:

#!/bin/bash
SOURCE="/home/user/important_data"
DEST="/mnt/backup/$(date +%Y-%m-%d)"
rsync -av --delete "$SOURCE/" "$DEST/"

The --delete flag removes files from the destination that no longer exist in the source, making it a true mirror. Schedule this with cron for automated backups.

Handling Large Directories And Performance Tips

Copying huge directories can be slow. Here are ways to speed things up:

  • Use rsync instead of cp for subsequent copies (only transfers changes).
  • Copy to a different disk or partition to avoid filesystem contention.
  • Use ionice to prioritize I/O if the system is busy.
  • For many small files, tar piped to ssh can be faster than rsync.

Example with ionice:

ionice -c 2 -n 7 rsync -av source/ destination/

This sets a low priority for the copy, reducing impact on other processes.

Copying Directories With Symlinks

By default, cp -r copies the target of symbolic links, not the links themselves. To copy the symlinks as links, use -d or -a:

cp -a source_dir dest_dir

The -a flag preserves symlinks. If you want to copy the actual files that symlinks point to, use -L:

cp -rL source_dir dest_dir

This dereferences symlinks and copies the target files.

Frequently Asked Questions

Q: What is the command to copy a directory in Linux?
A: Use cp -r source destination. The -r flag enables recursive copying of directories and their contents.

Q: How do I copy a directory and its contents in Linux?
A: Run cp -r /path/to/source /path/to/destination. This copies the entire directory tree, including subdirectories and files.

Q: How to copy a directory in Linux without overwriting existing files?
A: Add the -n flag: cp -rn source destination. This prevents overwriting any files that already exist in the destination.

Q: Can I copy a directory to a remote server in Linux?
A: Yes, use scp -r source user@remote:/path or rsync -av source/ user@remote:/path/. Both support recursive directory copies over SSH.

Q: How to copy a directory with permissions in Linux?
A: Use cp -a source destination. The -a flag preserves permissions, ownership, timestamps, and copies recursively.

Final Tips For Directory Copying

Always test your copy command with a small directory first if you’re unsure. Use the -v flag to see what’s happening. For critical data, verify the copy with diff or rsync -c.

Remember that copying directories over a network can be slow and may fail if the connection drops. For large transfers, use rsync with --partial to resume interrupted copies.

With these techniques, you can confidently copy any directory in Linux, whether it’s a simple backup or a complex migration. Practice the commands in a test environment until they become second nature.