How to cp 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 stands for 'copy'.
- To copy a file named `source.txt` to `destination.txt`, the command is `cp source.txt destination.txt`.
- To copy a directory named `sourcedir` to `destdir`, use `cp -r sourcedir destdir`.
- The `-i` option prompts before overwriting an existing file.
- The `-v` option provides verbose output, showing which files are being copied.
Overview
The `cp` command is a fundamental utility in Linux and other Unix-like operating systems. It is used to copy files and directories from one location to another. Understanding how to use `cp` effectively is crucial for managing files and organizing your system. Whether you're a beginner or an experienced user, a solid grasp of its options and syntax can save you time and prevent accidental data loss.
Basic File Copying
The simplest use of the `cp` command is to copy a single file. The basic syntax is:
cp [OPTIONS] SOURCE DESTINATION
Where:
SOURCEis the file you want to copy.DESTINATIONis where you want the copy to be placed. This can be a new filename or a directory.
Example 1: Copying a file to a new name in the same directory
If you have a file named report.txt and you want to create a backup named report_backup.txt in the same directory, you would use:
cp report.txt report_backup.txt
Example 2: Copying a file to a different directory
Suppose you want to copy document.pdf from your current directory to a directory named ~/Documents/Archive/:
cp document.pdf ~/Documents/Archive/
If the destination directory (~/Documents/Archive/ in this case) already exists, cp will copy the file into it, keeping its original name. If you want to rename it as part of the move, you can specify the new name:
cp document.pdf ~/Documents/Archive/old_document.pdf
Copying Directories
Copying directories requires a special option because a directory can contain multiple files and other subdirectories. Without this option, `cp` will only copy the directory entry itself, not its contents.
The most common options for copying directories are -r (recursive) or -R (also recursive, but behaves slightly differently with symbolic links on some systems; `-r` is generally preferred and more portable).
The syntax for copying a directory is:
cp -r SOURCE_DIRECTORY DESTINATION_DIRECTORY
Where:
SOURCE_DIRECTORYis the directory you want to copy.DESTINATION_DIRECTORYis where you want the copied directory to be placed.
Example 3: Copying a directory
Let's say you have a directory named project_files and you want to create a copy of it named project_files_backup in the same location:
cp -r project_files project_files_backup
This command will create a new directory called project_files_backup and copy all files and subdirectories from project_files into it.
Example 4: Copying a directory into another directory
If you want to copy project_files into an existing directory called ~/Backups/:
cp -r project_files ~/Backups/
This will result in ~/Backups/project_files/ containing all the contents of the original project_files directory.
Useful Options
The `cp` command has several options that enhance its functionality and control:
-i(interactive): Prompts for confirmation before overwriting an existing file. This is a safety feature to prevent accidental data loss.
cp -i important_file.txt backup_location/
-v (verbose): Explains what is being done. It lists each file as it is copied.cp -v *.jpg ~/Pictures/Vacation/
-p (preserve): Preserves the original file's attributes, such as modification time, access time, and ownership, if possible.cp -p config.ini /etc/
-u (update): Copies only when the source file is newer than the destination file or when the destination file is missing.cp -u ~/Downloads/new_software.tar.gz /opt/
--backup[=CONTROL]: Makes a backup of each existing destination file that would be overwritten. The CONTROL argument specifies how to name the backup (e.g., numbered, simple extension).Handling Permissions and Ownership
When copying files, the new files typically inherit the ownership and permissions of the user running the `cp` command. If you need to preserve the original ownership and permissions (e.g., when copying system files or performing administrative tasks), you might need to use the -p option. For system-wide copies or when running `cp` as a non-root user to copy files owned by root, you might need to use sudo:
sudo cp -p /path/to/original/file /path/to/destination/
Common Pitfalls and Tips
- Overwriting files: By default, `cp` will overwrite existing files in the destination without warning. Always use the
-ioption or be very careful with your destination paths. - Copying directories: Remember to use the
-ror-Roption when copying directories. Forgetting this will only copy the directory entry, not its contents. - Path names: Ensure you are using the correct source and destination paths. Relative paths are relative to your current working directory. Absolute paths start with
/. - Permissions: If you encounter 'Permission denied' errors, you might not have the necessary read permissions for the source file or write permissions for the destination directory. Using
sudomight be necessary for certain operations, but use it with caution. - Symbolic links: The behavior of `cp` with symbolic links can sometimes be confusing. By default, `cp` copies the file that the symbolic link points to. Use options like
-P(never follow symlinks),-L(always follow symlinks), or-H(follow symlinks only if they are command-line arguments) to control this behavior.
Mastering the `cp` command is a fundamental step in becoming proficient with the Linux command line. By understanding its basic syntax and utilizing its various options, you can efficiently manage your files and directories.
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 pagesfair-use
- Copying Files and Directories - GNU Coreutils ManualGPL-3.0-or-later
Missing an answer?
Suggest a question and we'll generate an answer for it.