Home Beginner's Guide 13 ways to use the copy command in Linux (with examples)

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

by Arun Kumar
copy command in Linux

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.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.