Cantech Knowledge Base

Your Go-To Hosting Resource

How to Copy Files and Directories in Linux Using the cp Command?

Manipulating files and directories is crucial in Linux. Be it backup creation, duplication of configurations, or migrating data files from one path to another, knowing how to copy files and directories is indeed the way to go.

The cp command is a basic and very powerful building block of Linux commands that you can use to copy files and directories from one site to another. In the Linux operating system, it is generally important for file management and is used, at times, by system administrators, developers, and even end users.

This guide will discuss using the cp command effectively, from basic to advanced examples of use, and much more for you to acquire Linux file management skills.

What is the cp Command?

The cp command is one of the most powerful built-in commands in Linux that facilitates file and directory cloning for the user. It permits users to copy a file or more than one file and directories together. It also preserves file attributes, symbolic links, and permissions if needed.

The Syntax of cp Command is as follows:

cp [options] source destination

Here, the source refers to the file or directory to be copied. Destination is the location of the file or directory. And, the Options define the extra flags that affect how the command works.

Common cp Command Options

Some of the common cp command options are listed as follows:

Options  Description

-i

Prompts the user before overwriting a file.

-r

Recursively copies directories and all files in that directory.

-v

Shows the actions being performed during the copying process.

-u

Copies only if the source file is newer than the destination file.

-p

Preserves file attributes like ownership and timestamps.

-a

Preserves the structure and attributes, as -p and -r.

–backup

Backs up the file after the original file is again created.  

Examples of Using the cp Command

Copying Files Using cp

1. To Copy a Single File

To copy a single file from one location to another, it is done easily using:

cp file1.txt /destination/path/

Use case: To make a copy of a configuration file in a backup location.

2. To Copy Multiple Files

To accomplish this, you need to copy multiple files in one step.

cp file1.txt file2.txt file3.txt /destination/path/

Use case: Copying multiple log files to an archive directory.

3. To Preserve File Metadata

To preserve the attributes of files, including timestamps and permissions, use the `-p` flag.

cp -p file.txt /destination/path/

Use case: To copy while leaving original metadata unchanged.

Copying directories using cp

1. To Copy Directory

This option allows copying directories.

cp -r folder/ /destination/path/

Use case: To create duplicates of a project folder.

2. Preserve Symbolic Links and Attributes

To preserve symbolic links and attributes when copying, use the `-a` flag:

cp -a folder/ /destination/path/

Use case: Copying a directory structure exactly.

Options for Overwriting and Prompting

1. To Overwrite Without Prompting

The `cp` command merely copies and overwrites files with no confirmation whatsoever:

cp file1.txt /destination/path/

Use case: Updating the already incomplete existing file with the new one.

2. Prompt Before Overwriting

To ask before overwriting, use -i.

cp -i file1.txt /destination/path/

Use case: Inadvertent overwrites and a way to avoid them.

3. Do not overwrite

The -n option does just the opposite, it does not replace existing files.

cp -n file1.txt /destination/path/

Use case: Copy without affecting already existing files.

Copying Files with Progress and Verbose Mode

1. To Display Copy Progress

Use the -v flag to see the files being copied:

cp -v file1.txt /destination/path/

Use Case: Verifying files are copied as expected.

2. To Copy Directories with Progress Display

For directories, combine -r with -v.

cp -rv source_directory/ /destination/path/

Use Case: Tracking the copying process of multiple files.

Advanced cp Usage

1. To Copy Only Newer Files

To copy files only if they are newer than the destination version, use -u:

cp -u file1.txt /destination/path/

Use Case: Synchronizing updated configuration files.

2. Copying Multiple Files with Wildcards

Copy all .txt files to another directory:

cp *.txt /destination/path/

Use Case: Moving all text files to a Documents folder.

3. Exclude Certain Files When Copying

rsync can be used as an alternative to avoid certain files.

rsync -av --exclude='file.txt' source_directory/ destination_directory/

Use Case: Backing up a directory while skipping certain files.

Wrapping Up

In Linux, the cp command is used specifically to copy files and directories. This includes simple procedures to copy single files to more involved operations of copying entire directory structures while providing file metadata or avoiding overwriting files. When mastered, the cp command becomes a powerful file management tool.

Now that you have finished reading the complete guide, you might have gained knowledge about copying files and directories in Linux with the cp command.

Feel free to contact us if you have any queries or require any assistance.

Frequently Asked Questions

1. Explain the basic function of the cp command for Linux.

The cp command is used to copy files and directories from one location to another.

2. Which command is used to copy multiple files in Linux?

The command to copy multiple files in Linux is as follows:

cp file1.txt file2.txt /path/to/destination/

3. Is it possible to copy a directory along with all files in it?

Absolutely yes, one can easily copy a directory using the -r flag along with the files in it.

cp -r folder /path/to/destination/

4. How do I copy files without overwriting existing ones?

Ensure that you are using the -n flag to copy files without overwriting existing ones.

5. How do I copy files while preserving timestamps and permissions?

Use the -p flag to preserve timestamps and permissions during copying.

cp -p file.txt /path/to/destination/
April 11, 2025