Being a Linux user, copying files and directories is one of the everyday tasks that you have to carry out. It can be copying a file to make a backup or copy it to another partition, directory, or external storage drive.
With Unix systems and all Linux distributions, you have two ways in which you can copy a file – using the graphical method and the command-line method. In this particular article, we shall look at the command-line method using the cp command.
CP stands for Copy in Linux systems. The command generally takes two main arguments – the file to copy and the destination to paste the file. This is illustrated with the syntax below:
- cp [file_to_copy] [destination_to_paste]
- cp [file_to_copy] [directory_to_paste]
- cp [file_to_copy_One] [file_to_copy_Two] [destination_to_paste]
- cp [option] [file_to_copy] [destination_to_paste]
Let’s take a look at the above commands. Option one and two are copying a file to a directory or another destination, such as an external media storage or partition. Option three, copies multiple files to a directory/folder. Lastly, option four copies a file to a directory but also enables users to add additional arguments like -v, which means verbose.
Below are 13 CP commands examples for Linux users:
1. Copy a file in the same directory but with a different name.
It is a method you can use to make a backup of a file in a different name. The syntax is as follows:
cp [file_to_copy] [file_new_name] cp testFile fileone
From the above image, when we run the ls command, we see that we have a created a duplicate of the testFile with the name fileOne.
2. Copy a single file into a new directory.
Suppose you want to copy a file and paste it to another directory, use the syntax below:
cp [source] [directory/] cp testFile UBUNTU
From the above image, we have a file called testFile in the ‘FOSSLINUX’ directory on the Desktop. We want to copy it to the UBUNTU directory inside the FOSSLINUX directory using the command in the image. By running the ls command on the UBUNTU directory, we see that we have successfully copied the file.
3. Copying multiple files into a directory
Suppose you have several files that you want to put in one directory, you don’t need to run a single command for each of them. Below is the syntax we shall use:
cp [file_to_copy_One] [file_to_copy_Two] [file_to_copy_Three] [destination_to_paste/] cp testFile testFile2 testFile3 UBUNTU/
From the image above, we have copied the files – testFile testFile2 and testFile3 to the UBUNTU directory with a single command.
4. Use the CP command to copy all files into a directory.
The previous command showed us how we could copy several files into a directory. The method is efficient only for a relatively small number of files since it might be time-consuming. If you want to copy all the contents present to a directory, there is even a more straightforward method – using the asterisk sign ‘*.’ See the syntax below:
cp [*] [destination directory] cp * /home/tuts/Desktop/FOSSTUTS/
From the above image, we have copied all the files in the FOSSLINUX directory on the Desktop to the FOSSTUTS directory.
5. Use the cp command to copy files interactively.
It is an efficient method to copy multiple files to a directory. If the destination directory has a similar file to that in the source directory, it gives you a warning asking you whether to overwrite the file. It is possible using the ‘-i’ parameter. See the syntax below:
cp -i [files_to_copy] [destination_directory] cp -i * /home/tuts/Desktop/FOSSTUTS/
From the above image, we are copying files from the FOSSLINUX directory to the FOSSTUTS directory. However, since the FOSSTUTS directory contains files similar to those in the source – FOSSLINUX directory, we get a prompt whether to overwrite the file. Enter ‘y’ for YES and ‘n’ for NO.
6. Use CP to copy files with the verbose(-v) option.
Like most Linux commands, you can use the CP command with the verbose ‘-v’ option. See the syntax below:
cp -v [files_to_copy] [destination_folder] cp -v testFile5 UBUNTU/
From the above image, we added the verbose parameter ‘-v,’ which gives us an output/report of the action we performed.
We can also use both verbose and interactive parameters in one command, as shown in the image below. The syntax is as follows:
cp -iv [files_to_copy] [destination_to_copy] cp -vi * /home/tuts/Desktop/FOSSTUTS/
7. Use CP to copy a Directory and its contents recursively.
To copy a directory to another destination, we need to use the r or R parameter, which stands for ‘recursive.’ The syntax is as follows:
cp [source_directory] [destination_directory] cp -R /home/tuts/Desktop/FOSSLINUX/* /home/tuts/Desktop/FOSSTUTS/
From the above image, we have copied all the contents of the FOSSLINUX directory to the FOSSTUTS directory. When we run the ls command on the FOSSTUTS directory, we see that it has contents similar to those in the FOSSLINUX directory.
8. Archive files and directories using the CP command.
Using the cp command, you can archive files and directories while copying them to the destination storage. We use the -a parameter. The syntax is as follows:
cp -a [files_to_copy] [destination_directory]
We shall use the command in the previous example only that we will add the -a parameter.
cp -a /home/tuts/Desktop/FOSSLINUX/* /home/tuts/Desktop/FOSSTUTS/
9. Copy only new files than those in the destination directory.
Suppose you always make copies of your files to another directory, you can make use of the -u parameter which only copies the newest files compared to those in the target destination. For example, we have two directories here, FOSSLINUX and FOSSTUTS.
Both have three similar files in them – testFile1 testFile2 and testFile3. We make changes to testFile1 in the FOSSLINUX directory and copy it to the FOSSTUTS directory using the -u and -v (verbose) parameter and see what happens. See the syntax below:
cp -u [source_directory] [destination_directory]
From the above image, we see that even though we gave the command for copying all files, only the file with the latest edits were copied.
10. Avoid overwriting existing files when using the cp command.
In the previous example, we talked of using the -i (interactive) parameter, which prompts you whether to overwrite existing files or not. However, if you don’t want to overwrite any file at all, there is a more straightforward method – using the -n parameter. See the syntax below:
cp -n [files_to_copy] [destination_directory]
Let’s look at the image below.
In the first command, we used the -i parameter, which prompts us whether to overwrite the testFile1. In the second command, we used the -n parameter, and it didn’t raise any prompts or overwrite the file. It is an efficient method when working with many files.
11. Create a symbolic link using the cp command.
Apart from copying files, you can also use the cp command to create a symbolic link. We shall use the -s parameter. The syntax is as follows:
cp -s [Link_file] [Target_File] cp -s /home/tuts/Desktop/FOSSLINUX/testFile1 /home/tuts/Documents/
As illustrated in the image above, we have created a symbolic link to the Documents directory using the testFile1 in the FOSSLINUX directory on the Desktop.
12. Create a hard link using the cp command.
Just as we have created a soft link in the previous example, we can also use the cp command to create a hard link. We shall use the -l parameter. See the figure below.
Unlike a soft link, with the hard link, the source and link file have the same inode numbers. Let’s run the ls -l command to verify.
13. Copying attributes of file/directory with cp command.
You can use the cp command to copy only the attributes of a file. See the syntax below:
cp [--attributes-only] [source_file] [destination_file]
From the image above, we copied the attributes of testFile2 in the FOSSLINUX directory to the FOSSTUTS directory. When we run the ls -l command, we can see the testFile2 in the FOSSLINUX directory has a size of 2773 bytes while the copied testFile2 in the FOSSTUTS directory has zero(0) bytes. It is because only the attributes of the file were copied and not the contents.
Conclusion
From the above examples, we see you can perform a lot more with the cp command other than copying files and directories. These commands can work in any Linux distribution you use, including Ubuntu, Debian, Fedora, and Arch Linux. Is there a cp command you would like to share with our readers? Feel free to post in the comment section below.