Learn How to Use the cp Command in Linux Using 7 Examples

cp-command

The cp command is utilized to copy files and directories. You can use the cp command in Linux to create a copy of a file, copy a file(s) to a directory, or copy a directory(s) to another. You will often need to run the cp command when using Linux for different needs, and understanding the various ways and options of using the cp command is helpful.

Throughout this post, we will discuss the cp command in detail. First, we will present all the options you can use with the command. Next, we will cover a few examples of the cp command.

The Syntax for the cp Command and Its Options

When running the cp command, the syntax will depend on what you want to copy. For instance, if copying files to a directory, use the below syntax.

Below are the commonly used cp command options.

OptionDescription
-vIt displays information on the copying process
-iIt enables the interactive mode before overwriting a file/directory.
-fIt forcefully replaces an existing file/directory.
-b or –backupIt is used when you want to create a backup of the existing files before overwriting them.
-rIt is used when you are copying a directory.
-lThis option creates a hard link instead of copying a file.
–remove-destinationThe option removes all files in the target destination before opening them

cp Command in Linux with Examples

We’ve presented some of the options you can use when running the cp command. Next, let’s give examples of how to copy files and directories while utilizing the presented options.

1. Copying a File

The obvious way of using the cp command is to create a copy of an existing file. First, use the Linux ls command to list the files in our current working directory.

We’ve then run the cat command to display the contents of our target file.

ls-and-cat-commands

To create a copy of the ‘names.txt,’ run the cp command and provide the new file’s name.

Next, verify that the file has been created and its contents match the source file’s contents.

cp-command-copy-file

2. cp Command Copy File to Directory

Instead of creating a copy of a file, you can copy the file to a source destination. We follow the same syntax, but the destination will be an existing directory.

For this example, we are copying two files.

copy-file-to-directory

Our directory contains the copied files, and we’ve verified them by running the ls command.

verify-copied-directory

3. Copying a Directory

So far, we’ve only demonstrated how to copy files. However, you can also copy a directory to a destination directory.

Unlike when copying files, we must add the -r option to signal to the cp command that we are copying a directory.

Here’s an example. We’ve added the -v option to display information about the copying process.

You will get an output showing that the copy process has been completed.

cp-command-copy-directory

Moreover, you can run the ls command and verify that we’ve managed to copy the file and that it contains the same contents as the source directory.

verify-copied-directory

4. How to Use the cp Command Interactively

If you are unsure whether the source destination contains content similar to those you want to copy and don’t want to overwrite, add the -i option. You will get prompted to overwrite the existing files to avoid losing your data.

The example below tries copying a directory to a source, and since the directory already exists, we get prompted to confirm what action to take. To overwrite it, type ‘yes’ and press the enter key; otherwise, press ‘no’ to cancel the process.

cp-command-interactive-copying

5. Creating a Backup Before Overwriting

Sometimes, you may have an instance where you want to overwrite existing files but don’t want to lose them. A better approach is to back up existing files and then overwrite them.

To backup files, use the -b or –backup option. We’ve used numbering when creating the backup for all text files.

cp-command-create-backup

When you list the contents of the target destination, you will note that the backup files exist. However, we’ve overwritten the existing files, and the backup acts as a way of storing the files.

list-created-backup-files

6. cp Command Force Overwrite

When you don’t have the write permissions, trying to overwrite existing files when running the cp command will throw a “permission denied” error.

cp-command-permission-denied

However, you can forcefully implement overwriting the existing files by adding the -f option in your cp command.

cp-command-force-overwrite

7. Creating a Hard Link

When we mention links, the best way to create them is by using the ln command, and we have a tutorial on using the ln command in Linux. It’s possible to create a hard link for a file instead of copying it when running the cp command.

Use the -l option in your command to create a hard link.

cp-command-create-hard-link

Although the output seems like we’ve created a copy of the file, we can check the inodes to verify that it’s a hard link. Run the below ls command with the -i option.

If the inodes for the files match, like in our case, it confirms that it is a hard link.

verify-created-hard-link
Read Also: How to move files using the 'mv' command in Linux.

Conclusion

The cp command helps with copying files and directories. Whether you want to create a copy of a file, copy a file to a directory, or copy a directory to another directory, the cp command has you covered.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top