cp Command in Linux (original) (raw)

Last Updated : 11 Apr, 2026

The cp (copy) command in Linux is used to duplicate files or directories from one location to another within the file system. It supports copying single files, multiple files, and entire directories, with options to control overwriting and attribute preservation.

It offers three principal modes of operation, each serving different purposes.

1. Copying Between Two Files in Linux

When the cp command is provided with two file names, it copies the contents of the source file to the destination file.

cp Sorce_file Destination_file

**Example 1: Copy to a New File

Create a new file by copying the contents of an existing file.

cp a.txt b.txt

Copy a file in Linux

copy a file in Linux

We used `ls` command to display all the file in the current directory.

**Example 2: Overwrite an Existing File

Replace the contents of an existing file with another file’s contents.

cp a.txt c.txt

Copy a file in Linux

Copy a file in Linux

We used `ls` command to display all the file in the current directory and used `cat`command to display the content in the text file.

Syntax

The cp command has a flexible syntax depending on whether you are copying a single file, multiple files, or directories.

cp [options]
cp [options] ...

2. Copy files to a Directory in Linux

When the cp command is given one or more source files followed by a destination directory, it copies each source file into the destination directory using the same file names.

cp Src_file1 Src_file2 ... Dest_directory

**Example: Copy Multiple Files to a Directory

Copy several files into a single directory in one command.

cp a.txt b.txt c.txt new/

Copy multiple files to another directory

Copy multiple files to another directory

We used `ls` command to display all the file in the "new" directory to confirm the successful copy of file in that directory.

3. How to Copy Directories in Linux

cp -R Src_directory Dest_directory

copying files between two directories

copying files between two directories

**Behavior Details

Options Available in cp Command

1. -i (Interactive Mode) – Prompt Before Overwriting Files

The -i option enables interactive mode for the cp command. In this mode, cp asks for user confirmation before overwriting an existing destination file. By default, cp overwrites files silently. The -i option changes this behavior by introducing a safety check.

**Syntax:

cp -i Source_file Destination_file

**Example:

cp -i a.txt b.txt

Copy a File in Linux Using `-i`

Copy a File in Linux Using `-i`

Here,

2. -f (Force Mode) – Overwrite Files Without Prompt

The -f option enables force mode for the cp command. In this mode, cp overwrites the destination file without asking for confirmation, even if it has restrictive permissions. By default, if cp cannot write to the destination file due to permissions, it fails. The -f option deletes the destination file first (if needed) and then copies the source file.

**Syntax:

cp -f Source_file Destination_file

**Example:

cp -f a.txt b.txt

Copy a File in Linux Using `-f`

Copy a File in Linux Using `-f`

3. -r / -R (Recursive Mode) – Copy Directories and Subdirectories

The -r or -R option enables recursive copying in the cp command. With this option, cp can copy an entire directory along with all its subdirectories and files. By default, cp cannot copy directories without this option. Using -r ensures that the complete directory structure is preserved in the destination.

**Syntax:

cp -r Source_directory Destination_directory

**Example:

cp -r geeksforgeeks gfg

cp-r

4. -p (Preserve Attributes) – Retain File Permissions, Ownership, and Timestamps

The -p option preserves important file attributes while copying.

These attributes include:

By default, cp creates a new file with default attributes. Using -p ensures the copy maintains the same characteristics as the source file.

**Note: For the preservation of characteristics, you must be the root user of the system, otherwise characteristics change.

**Syntax:

cp -p Source_file Destination_file

**Example:

cp -p d.txt b.txt

cp-p

5. * (Wildcard) – Copy Multiple Files Matching a Pattern

The * wildcard allows the cp command to select and copy multiple files at once based on a pattern. This is especially useful when you want to copy files with a common extension or naming convention without listing each file individually.

**How it works:

**Syntax:

cp *.txt Destination_directory

**Example:

cp *.txt new/

Copy a File in Linux Using `*`

Copy a File in Linux Using `*`