Skip to main content

Linux Commands Series

cp Command in Linux

In this article, we demonstrate how to copy files and folders using the cp command in Linux through practical examples you can try right away.

Moving onward, let's discuss a popular and commonly used Linux command for copying files and directories, known as the β€œcp” command.

The cp command is used to copy files and directories at a given path. It is preferred by most Linux users due to its simplicity and rich functionality.

Let’s understand the usage of the cp command using practical examples that can be used on a day-to-day basis.

Syntax of cp Command

The syntax of the cp command is identical to other Linux commands. At a high level, it is divided into two parts – options and arguments:

cp [OPTIONS] <SOURCE> <DEST>
cp [OPTIONS] <SOURCE-1> <SOURCE-2> ... <DIRECTORY>

In the above syntax:

  • Square brackets [] represent optional arguments
  • Angular brackets <> represent mandatory arguments

1. How to Copy a File in Linux

One of the basic uses of the cp command is to copy a file into the current directory. This operation is commonly performed to take a backup of important configuration files.

For example, before updating the SSH configuration, users often create a backup copy of the /etc/ssh/sshd_config file.

To understand the usage better, let’s start by creating a simple file:

touch file-1.txt

Next, create a copy of the file using the following command:

cp file-1.txt file-2.txt
Copy File in Linux

2. Show Copy Command Progress

In the previous example, we used the ls command to check whether the file was copied successfully. However, using an additional command just to verify the result may seem unnecessary.

Instead, we can enable verbose mode using the -v option with the cp command. This displays a message for each file being copied, making it easier to track the progress of the operation directly.

For example, to copy file-1.txt to a new file named file-3.txt with progress output, run:

cp -v file-1.txt file-3.txt
Show Copy Output

In the output:

  • The left side (file-1.txt) represents the source file.
  • The right side (file-3.txt) is the destination file.
  • The arrow (->) indicates the copy operation from source to destination.

This is especially helpful when copying multiple files, as you can monitor each step without needing to manually check afterward.

3. How to Copy Multiple Files to the Directory

So far, we’ve worked with single files in the current directory. But in real-world scenarios, especially in production environments, it's common to copy multiple files into a single directory.

While you could run the cp command multiple times, that’s inefficient. Instead, you can use this more effective syntax.

Step 1: Create a target directory

mkdir dir-1

Step 2: Copy multiple files to that directory

cp -v file-1.txt file-2.txt file-3.txt dir-1
Copy Multiple Files in Linux

The above output shows that all files have been copied to the dir-1 directory. Also, it is important to note that, to use this alternative syntax the directory must be present already and it must be the last argument of the command.