13 ways to use the copy command in Linux (with examples)

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. Not just copying, you can perform a lot more with the cp command other than copying files and directories. Let's get into details.

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

copy a file with to a directory in a different name.
copy a file with CP to a directory in a different name.

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

copy a file into a directory
copy a file into a directory

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/

Copying multiple files into a directory
Copying multiple files into a directory

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/

Copy ALL files into a directory
Copy ALL files into a directory

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/

Copy Files Intercatively with -i parameter
Copy Files Interactively with -i parameter

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/

Copying files with the Verbose option
Copying files with the Verbose option

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/

Using both verbose and interactive method
Using both verbose and interactive method

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/

Copying a directory recursively
Copying a directory recursively

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/

Archiving files using cp command
Archiving files using cp command

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]

copy only newest files
copy only newest files

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.

copy files without over-writingng using -n parameter
copy files without over-writing using -n parameter

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/

create symbolic link with cp command
create a symbolic link with cp command

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.

Create a hardlink using cp command
Create a hard link using cp command

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]

copying attributes-only of a file
copying attributes-only of a 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.

Arun Kumar
Arun did his bachelor in computer engineering and loves enjoying his spare time writing for FOSS Linux. He uses Fedora as the daily driver and loves tinkering with interesting distros on VirtualBox. He works during the day and reads anything tech at night. Apart from blogging, he loves swimming and playing tennis.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,241FansLike
389FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

The Ubuntu Cinnamon Remix brings together Linux Mint's Cinnamon desktop with the Ubuntu Core. While some users are welcoming the new flavor of Ubuntu with open arms, others are scratching their heads, wondering where it fits in.
The wait is finally over (almost) for all you Ubuntu fans out there. The latest version of Ubuntu, 20.10 codenamed "Groovy Gorilla," is currently available in the beta version. I have tested out the distro myself, and it is stable enough to take out for a spin.

Top 20 must-have apps for your Ubuntu PC

OK, this one is going to be a long one, so grab a cup of coffee and scroll through the best apps that we think are must-have for your Ubuntu PC. We have hand-picked each one of these considering the most common categories that suit an average Linux user.

Manjaro vs. Ubuntu – which is better for you?

If you are a person associated with Computer technology and spend most of the time in the open-source arena, you must have heard or worked with some of the popular Linux distributions we have in the market. Some of the names that you will never miss are; Ubuntu, Arch Linux, Debian, and Mint.

5 Best Application Launchers for Ubuntu

Ubuntu is one of the most used Linux distributions worldwide. It is also the reason why it has the maximum number of available programs for itself. Today we are going to talk about one category of those programs, the application launchers.

Linux Mint Cinnamon Vs. MATE: which one to choose?

Linux Mint is by far one of the most popular Linux distros on the market, especially among Windows users who are jumping into the Linux bandwagon. This is mostly because Linux Mint comes with a familiar desktop environment that resembles the classic Windows desktop. It offers tons of quality of life features, making it very user-friendly for users who have never tried Linux before.