Linux cp Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux cp command to copy files and directories. You will start by understanding the basic usage of the cp command, including copying files and directories, and then explore more advanced features such as preserving file attributes and timestamps. The lab covers practical examples to help you become proficient in managing files and directories using the cp command.

The lab is divided into three main steps:

  1. Understand the Basics of the cp Command
  2. Copy Files and Directories Using the cp Command
  3. Preserve File Attributes and Timestamps with the cp Command

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") subgraph Lab Skills linux/mkdir -.-> lab-422615{{"`Linux cp Command with Practical Examples`"}} linux/ls -.-> lab-422615{{"`Linux cp Command with Practical Examples`"}} linux/cp -.-> lab-422615{{"`Linux cp Command with Practical Examples`"}} linux/touch -.-> lab-422615{{"`Linux cp Command with Practical Examples`"}} end

Understand the Basics of the cp Command

In this step, you will learn the basic usage of the cp command in Linux. The cp command is used to copy files and directories from one location to another.

Let's start by creating a sample file in the ~/project directory:

touch ~/project/sample.txt

Now, let's copy the sample.txt file to a new file named copy_of_sample.txt:

cp ~/project/sample.txt ~/project/copy_of_sample.txt

Example output:

The basic syntax of the cp command is:

cp [options] source_file destination_file

Here, source_file is the file you want to copy, and destination_file is the new file that will be created.

You can also use the cp command to copy directories. Let's create a directory named dir1 and copy it to dir2:

mkdir ~/project/dir1
cp -r ~/project/dir1 ~/project/dir2

The -r option is used to copy directories recursively, including all the files and subdirectories within the directory.

Example output:

That's the basic usage of the cp command. In the next steps, you will learn more advanced features of the cp command, such as preserving file attributes and timestamps.

Copy Files and Directories Using the cp Command

In this step, you will learn how to copy files and directories using the cp command, including copying multiple files and directories at once.

Let's start by creating some sample files and directories in the ~/project directory:

touch ~/project/file1.txt ~/project/file2.txt
mkdir ~/project/dir1 ~/project/dir2

Now, let's copy multiple files at once:

cp ~/project/file1.txt ~/project/file2.txt ~/project/dir1

This will copy file1.txt and file2.txt to the dir1 directory.

Example output:

You can also copy an entire directory and its contents using the -r (recursive) option:

cp -r ~/project/dir1 ~/project/dir3

This will create a new directory dir3 and copy all the contents of dir1 into it.

Example output:

If you want to copy a directory and rename it at the same time, you can use the following syntax:

cp -r ~/project/dir1 ~/project/dir4

This will create a new directory dir4 and copy all the contents of dir1 into it.

Example output:

As you can see, the cp command provides a flexible way to copy files and directories in Linux. In the next step, you will learn how to preserve file attributes and timestamps when copying files.

Preserve File Attributes and Timestamps with the cp Command

In this step, you will learn how to preserve file attributes and timestamps when copying files using the cp command.

By default, the cp command will copy the file contents, but it may not preserve the original file attributes and timestamps. To preserve these, you can use the -p (preserve) option.

Let's create a sample file with specific attributes and timestamps:

touch -a -m -t 202304010000 ~/project/sample.txt

This will create the sample.txt file with the access and modification times set to April 1, 2023, 00:00.

Now, let's copy the file while preserving its attributes and timestamps:

cp -p ~/project/sample.txt ~/project/copy_of_sample.txt

Example output:

To verify that the attributes and timestamps were preserved, you can use the ls -l command:

ls -l ~/project/sample.txt ~/project/copy_of_sample.txt

Example output:

-rw-r--r-- 1 labex labex 0 Apr  1 00:00 ~/project/sample.txt
-rw-r--r-- 1 labex labex 0 Apr  1 00:00 ~/project/copy_of_sample.txt

As you can see, the access and modification times of the copied file are the same as the original file.

The -p option preserves the following attributes:

  • Ownership
  • Permissions
  • Timestamps (access, modification, and change)
  • SELinux security context (if present)

This is useful when you need to maintain the original file properties during the copy operation.

In the next step, we will summarize what you have learned about the cp command.

Summary

In this lab, you learned the basic usage of the cp command in Linux, which is used to copy files and directories from one location to another. You started by creating a sample file and copying it to a new file. You also learned how to copy directories recursively using the -r option. Additionally, you explored copying multiple files and directories at once, which can save time and simplify the copying process. The lab provided practical examples to help you understand the various capabilities of the cp command and how to effectively use it in your daily Linux workflows.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like