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
Key Facts
- The `cp` command is used for copying files and directories.
- The `-r` or `-R` option is essential for copying directories recursively.
- The `-v` option can be added for verbose output, showing each file being copied.
- The `-a` option is a combination of `-dR --preserve=all` and is useful for archiving.
- Be mindful of permissions; `cp` by default may not preserve them unless `-p` is used or `-a` is chosen.
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_directoryLet's break down this command:
cp: The command itself.-ror-R: The recursive flag. This is crucial for copying directories.source_directory: The path to the directory you want to copy.destination_directory: The path where you want the copy to be placed.
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:
- `-v` (verbose): This option provides a detailed output, showing each file and directory as it is being copied. This is very helpful for monitoring the progress of large copies or for troubleshooting.
cp -rv my_project ~/backups/- `-a` (archive): This is a powerful option that is often preferred for copying directories, especially for backups. The `-a` option is equivalent to `-dR --preserve=all`. It means it will copy recursively, preserve symbolic links as themselves rather than copying the files they point to, and preserve file attributes such as modification times, access times, ownership, and permissions as much as possible.
cp -a my_project ~/backups/- `-p` (preserve): This option preserves the modification time, access time, and ownership of the source files. It's often used in conjunction with `-r`. Note that `-a` includes `-p` and more.
cp -rp source_directory destination_directory- `--parents: This option creates the destination directories as needed. If you copy a file like `dir1/dir2/file.txt` with `--parents`, it will create `dir1/` and `dir1/dir2/` in the destination if they don't exist. This is particularly useful when you want to replicate the directory structure from the source path.
cp --parents source_directory/some_file.txt destination_directory/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_directoryConversely, if you want to ensure that existing files are *not* overwritten, you can use the `-n` (no-clobber) option.
cp -rn source_directory destination_directorySymbolic 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.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- Cp (Unix) - WikipediaCC-BY-SA-4.0
- cp(1) - Linux man pagesCC-BY-SA-4.0
Missing an answer?
Suggest a question and we'll generate an answer for it.