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.
$ cp [options] [file] [destination]
Below are the commonly used cp command options.
Option | Description |
-v | It displays information on the copying process |
-i | It enables the interactive mode before overwriting a file/directory. |
-f | It forcefully replaces an existing file/directory. |
-b or –backup | It is used when you want to create a backup of the existing files before overwriting them. |
-r | It is used when you are copying a directory. |
-l | This option creates a hard link instead of copying a file. |
–remove-destination | The 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.
To create a copy of the ‘names.txt,’ run the cp command and provide the new file’s name.
$ cp source_file destination_file
Next, verify that the file has been created and its contents match the source file’s contents.
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.
$ cp names.txt new1.txt Files/
Our directory contains the copied files, and we’ve verified them by running the ls command.
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.
$ cp -rv Files/ /home/linuxgeekery/Downloads/
You will get an output showing that the copy process has been completed.
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.
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.
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 -v --backup=numbered *.txt Files/
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.
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.
However, you can forcefully implement overwriting the existing files by adding the -f option in your cp command.
$ cp -f [source] [destination]
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 -lv names.txt new1.txt
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.
$ ls -i1
If the inodes for the files match, like in our case, it confirms that it is a 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.