How to cp directory in linux

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: In Linux, you can copy a directory and its contents using the `cp` command with the `-r` or `-R` (recursive) option. For example, `cp -r source_directory destination_directory` will copy the entire `source_directory` to the `destination_directory`.

Key Facts

Overview

The process of copying directories in Linux is a fundamental operation for managing files and data. Whether you need to duplicate a configuration folder, back up project files, or move data to a new location, the command line provides efficient tools to achieve this. The primary command for copying in Linux is `cp`.

Understanding the `cp` Command

The `cp` command, short for 'copy', is a standard Unix and Linux utility used to copy files and directories from one location to another. By default, `cp` is designed to copy files. To copy directories, you need to instruct the command to copy not just the directory itself, but also all the files and subdirectories contained within it. This is where the recursive option comes into play.

Copying a Directory Recursively

To copy a directory and its entire contents (files, subdirectories, and their contents), you must use the recursive option. The most common options are `-r` or `-R`. Both options achieve the same result: they tell `cp` to traverse the directory structure and copy each item it encounters.

Basic Syntax:

cp -r source_directory destination_directory

Let's break down this command:

Example:

Suppose you have a directory named `my_project` in your current directory and you want to copy it to a directory called `backups` located in your home directory.

cp -r my_project ~/backups/

This command will create a copy of `my_project` inside the `~/backups/` directory. If `~/backups/` does not exist, `cp` will create it. If `~/backups/` already exists, `my_project` will be copied inside it.

Useful Options for Copying Directories

While `-r` is the core option, `cp` offers several other flags that can enhance the copying process:

Important Considerations

Permissions and Ownership

When copying directories, especially with the basic `cp -r` command, the copied files and directories might inherit permissions and ownership from the user running the command, rather than preserving the original ones. If maintaining original permissions and ownership is critical, use the `-p` or, preferably, the `-a` option.

Overwriting Existing Files/Directories

If the destination directory already contains files or subdirectories with the same names as those being copied, `cp` will, by default, overwrite them without asking. To be prompted before overwriting, you can use the `-i` (interactive) option.

cp -ri source_directory destination_directory

Conversely, if you want to ensure that existing files are *not* overwritten, you can use the `-n` (no-clobber) option.

cp -rn source_directory destination_directory

Symbolic Links

By default, `cp -r` will copy the *contents* of the files that symbolic links point to. If you want to copy the symbolic links themselves (so the destination has links pointing to the same targets), you need to use the `-L` or `-d` option. The `-a` option handles symbolic links correctly by default.

Destination Path

The behavior of `cp` can subtly change based on whether the destination path exists and whether it's a directory. If `destination_directory` exists and is a directory, `source_directory` will be copied *inside* it. If `destination_directory` does not exist, `cp` will create it as a copy of `source_directory`.

Conclusion

The `cp -r` command is the standard and most common way to copy directories in Linux. For most use cases, especially backups or exact duplication, the `cp -a` command offers a more robust solution by preserving file attributes and handling symbolic links appropriately. Always consider the importance of permissions, ownership, and potential overwrites when performing copy operations.

Sources

  1. Cp (Unix) - WikipediaCC-BY-SA-4.0
  2. cp(1) - Linux man pagesCC-BY-SA-4.0

Missing an answer?

Suggest a question and we'll generate an answer for it.