How do I create a copy of a directory in Unix/Linux?
Categories:
Mastering Directory Copying in Unix/Linux: A Comprehensive Guide

Learn the essential commands and techniques for efficiently copying directories and their contents in Unix and Linux environments, including handling permissions and symbolic links.
Copying directories is a fundamental operation in Unix and Linux systems. Whether you're backing up data, moving projects, or setting up new environments, understanding how to correctly duplicate directories and their contents is crucial. This article will guide you through the primary methods using the cp
command, explain important options, and provide practical examples to ensure your files are copied exactly as intended.
The cp
Command: Your Primary Tool
The cp
command (short for 'copy') is the standard utility for copying files and directories. When copying directories, it's essential to use specific options to ensure that all subdirectories, files, and their attributes are preserved. Without the correct options, cp
will typically refuse to copy a directory, or it will only copy the directory itself without its contents.
cp -r source_directory destination_directory
Basic command to recursively copy a directory.
Understanding Key Options for Directory Copying
The cp
command offers several options that are particularly useful when dealing with directories. The most common and important ones are -r
(or --recursive
) and -a
(or --archive
).
cp
commands, especially when dealing with critical data. A typo could lead to unintended data loss or corruption.flowchart TD A[Start Copy Operation] --> B{Is Source a Directory?} B -->|No| C[Copy File (cp source dest)] B -->|Yes| D{Use -r or -a?} D -->|No| E[Error: Cannot copy directory] D -->|Yes, -r| F[Recursively Copy Contents] D -->|Yes, -a| G[Archive Copy (Preserve Attributes)] F --> H[End] G --> H[End]
Decision flow for copying files vs. directories with cp
.
Recursive Copying with -r
The -r
or --recursive
option is mandatory for copying directories. It tells cp
to copy directories and their contents recursively. This means it will copy all files and subdirectories within the source directory to the destination.
# Create a sample directory structure
mkdir -p my_project/src my_project/docs
echo "Hello World" > my_project/src/main.c
echo "README" > my_project/docs/README.md
# Copy 'my_project' to 'my_project_backup'
cp -r my_project my_project_backup
# Verify the copy
ls -R my_project_backup
Example of using cp -r
to copy a directory.
Archive Copying with -a
The -a
or --archive
option is a more comprehensive way to copy directories. It's equivalent to -dR --preserve=all
, meaning it recursively copies directories, preserves symbolic links, and attempts to preserve all file attributes (mode, ownership, timestamps, context, links, xattr). This is often the preferred method for backups or when you need an exact duplicate of a directory structure.
# Create a sample directory with specific permissions and a symlink
mkdir -p original_data
touch original_data/file1.txt
chmod 700 original_data
ln -s file1.txt original_data/link_to_file1
# Copy with -a
cp -a original_data archived_data
# Compare attributes (e.g., permissions)
ls -ld original_data archived_data
Using cp -a
to create an archive copy, preserving attributes and symbolic links.
cp -a
, if the destination directory already exists, the source directory's contents will be copied into the existing destination. If the destination does not exist, cp
will create it.Practical Scenarios and Advanced Usage
Beyond basic copying, cp
can handle various scenarios. For instance, copying only the contents of a directory (not the directory itself) or handling existing files.
1. Copying Directory Contents Only
To copy only the contents of source_directory
into an existing destination_directory
, use a trailing slash on the source or a wildcard. This prevents the source_directory
itself from being created as a subdirectory within the destination.
2. Overwriting Existing Files
By default, cp
will overwrite existing files in the destination if you have write permissions. To be prompted before overwriting, use the -i
(interactive) option. To force overwrite without prompting, use -f
. To avoid overwriting existing files, use -n
(no clobber).
3. Verbose Output
For larger copies, you might want to see what files are being copied. The -v
(verbose) option will print the name of each file as it is copied.
# Copy contents of 'source_dir' into 'target_dir'
mkdir source_dir target_dir
touch source_dir/fileA.txt
cp -r source_dir/* target_dir/
# Interactive copy (prompts before overwrite)
touch target_dir/fileA.txt # Create a conflicting file
cp -i source_dir/fileA.txt target_dir/
# Verbose copy
cp -rv source_dir target_dir_verbose
Examples of copying directory contents, interactive mode, and verbose output.