cp -r


cp -r (recursive copy for directories)

The cp -r command extends the cp utility to copy entire directory structures. The -r or -R option stands for "recursive," meaning it copies a directory, all of its files, and all of its subdirectories.


Example 1: Copy a directory to another location

# Recursively copy the 'project_x' directory into the 'backups' directory
cp -r project_x backups/

Explanation: This command creates a new directory named project_x inside the backups directory. It then copies all contents from the original project_x, including all files and subdirectories, into backups/project_x.


Example 2: Copy the contents of a directory

# Copy all files and subdirectories from 'source_code' into the 'build' directory
cp -r source_code/. build/

Explanation: The trailing dot and slash (/.) on the source directory is a crucial pattern. It tells cp to copy the contents of source_code into the build directory, rather than copying the source_code directory itself.


Example 3: Recursive copy with preserved attributes

# Recursively copy the 'config' directory, preserving all file attributes
cp -rp /etc/httpd/conf.d/ /opt/httpd_backup/

Explanation: Combining -r (recursive) with -p (preserve) is a common practice for creating complete and accurate backups of directories. This command copies the conf.d directory, ensuring all files within retain their original permissions, ownership, and timestamps.


Example 4: Recursive copy with verbose output

# Recursively copy the 'data' directory and show each file being copied
cp -rv data/ /mnt/nas/

Explanation: Using -rv provides a file-by-file report of the copy operation. This is useful for monitoring the progress of large recursive copies and for debugging permission issues.


Example 5: Interactive recursive copy

# Recursively copy 'my_app', prompting before overwriting any existing file
cp -ri my_app /var/www/

Explanation: The -ri options combine the recursive copy with an interactive prompt. Before cp overwrites any file in the destination, it will ask for your confirmation. This can prevent accidental data loss but can be tedious for large directories.


Example 6: Creating a complete archive copy (RHEL vs other UNIX)

# In RHEL (GNU), archive a user's home directory, preserving everything including links
cp -a /home/user /home/user.bak

Explanation: The -a (archive) option in RHEL is a powerful alias for -dR --preserve=all. It is equivalent to -rp but also preserves symbolic links, making it ideal for creating a complete, faithful backup of a directory.

UNIX Variant Commands: The -a flag is a GNU extension and may not be available on other UNIX systems.

macOS/BSD: The equivalent command to achieve a similar archive-style copy is cp -pPR /home/user /home/user.bak. The -P option prevents following symbolic links.

Solaris: A similar command would be cp -pr /home/user /home/user.bak. You may need to handle symbolic links separately depending on the desired outcome.


Example 7: Recursive copy with update option

# Recursively copy 'assets' to 'public', only copying new or updated files
cp -ru assets/ public/

Explanation: Combining -r (recursive) with -u (update) provides an efficient way to synchronize two directories. It recursively traverses the assets directory and only copies files to public that are either missing or are newer in the source directory.