Commands.page Logo

How to Copy Directory Preserving Attributes and Links in Ubuntu

This guide explains the specific command required to duplicate folders within the Ubuntu operating system while maintaining file permissions, ownership, timestamps, and symbolic links. We will cover the primary utility used for this task, the necessary flags to ensure data integrity, and provide clear examples for immediate implementation in your terminal.

The Command to Use

The standard command to copy a directory while preserving all attributes and links is cp with the -a flag. The -a option stands for “archive” and ensures that the copy is an exact replica of the source, including symbolic links, file permissions, ownership, and timestamps.

Syntax and Usage

The basic syntax for this command is as follows:

cp -a /source/directory /destination/path

Replace /source/directory with the path of the folder you want to copy and /destination/path with the location where you want the copy to reside. If the destination directory does not exist, cp will create it.

Why Use the -a Flag

Using the -a flag is crucial because it combines several other options into one command. It is equivalent to using -dR --preserve=all. This ensures:

Example Scenario

If you need to backup a configuration folder located at /etc/myapp to your home directory while keeping all settings intact, you would run:

sudo cp -a /etc/myapp ~/myapp_backup

Using sudo may be necessary if the source directory contains files owned by the root user. This command creates a myapp_backup folder in your home directory that is identical to the original in every structural and attribute aspect.