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

Quick Answer: The `cp` command in Linux is used to copy files and directories. To copy a file, you specify the source file and the destination. To copy a directory recursively, you use the `-r` or `-R` option.

Key Facts

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:

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:

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:

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

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.

Sources

  1. Cp (Unix) - WikipediaCC-BY-SA-4.0
  2. cp(1) - Linux man pagesfair-use
  3. 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.