How to cp directory
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 stands for 'copy'.
- The `-r` or `-R` option is essential for copying directories recursively.
- If the destination directory does not exist, `cp -r` will create it.
- If the destination directory already exists, `cp -r` will copy the source directory inside it.
- The command preserves file permissions and ownership by default, but this can be modified with other options.
Overview
The `cp` command is a fundamental utility in Unix-like operating systems, including Linux and macOS, used for copying files and directories. While copying a single file is straightforward (e.g., `cp file1 file2`), copying an entire directory and all its contents requires a specific option. This is because directories can contain other directories (subdirectories) and files, creating a hierarchical structure that needs to be replicated accurately.
Copying Directories with `cp -r`
To copy a directory and all its contents (files and subdirectories), you must use the recursive option, typically `-r` or `-R`. The `-r` option tells `cp` to traverse the directory structure and copy each item it encounters. Without this option, `cp` will refuse to copy a directory, usually resulting in an error message like "cp: -r not specified; omitting directory 'source_directory'".
Syntax
The basic syntax for copying a directory recursively is:
cp -r source_directory destination_directorysource_directory: The name of the directory you want to copy.destination_directory: The path where you want the copy to be placed.
Behavior Scenarios
The behavior of `cp -r` depends on whether the destination directory exists:
- If
destination_directorydoes not exist: `cp -r` will create a new directory nameddestination_directoryand copy the contents ofsource_directoryinto it. The resulting structure will bedestination_directory/source_directory_contents. - If
destination_directoryalready exists: `cp -r` will copy thesource_directoryitself and its contents into the existingdestination_directory. The resulting structure will bedestination_directory/source_directory/source_directory_contents.
For example, if you have a directory named project_a and you run:
cp -r project_a backups/And the backups directory already exists, you will end up with a structure like backups/project_a/ containing all the files and subdirectories from the original project_a.
Important Considerations and Options
While `-r` is the most common option, `cp` offers other flags that can be useful:
- `-v` (verbose): This option prints the name of each file and directory as it is copied. This is helpful for tracking the progress of large copies or for debugging. Example:
cp -rv source_dir dest_dir. - `-p` (preserve): This option preserves the original file attributes, including ownership, permissions, and timestamps. This is often desirable when creating backups or migrating data to ensure the copied files retain their original metadata. Example:
cp -rp source_dir dest_dir. - `-a` (archive): This option is equivalent to `-dR --preserve=all`. It implies recursive copying (`-r`), copying symbolic links as themselves rather than the files they point to (`-d`), and preserving all file attributes (`-p`). It's often the preferred option for creating exact backups. Example:
cp -a source_dir dest_dir. - `-u` (update): This option copies only when the source file is newer than the destination file or when the destination file is missing.
- `-i` (interactive): Prompts before overwriting an existing file.
- `-f` (force): Overwrites existing files without prompting. Use with caution.
Handling Large Directories
For very large directories or when copying over a network, `cp` might not be the most efficient tool. Utilities like `rsync` are often preferred because they can perform incremental transfers (only copying changed parts of files) and offer more advanced features for managing large-scale data synchronization.
Summary
In summary, the key to copying a directory with the `cp` command is the `-r` (recursive) flag. Combine it with other options like `-v`, `-p`, or `-a` as needed to control the copying process and preserve file attributes. Always double-check your source and destination paths to avoid accidental data loss or overwrites.
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 pagefair-use
- cp - copy files and directories - Linux commandsfair-use
Missing an answer?
Suggest a question and we'll generate an answer for it.