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,437FansLike
374FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to change Hostname on Ubuntu

Generally, a hostname is just an assigned name to a computer, laptop, or any other connected device to a network. You can say a hostname is a computer's nickname, and it is used to identify the machine over the network uniquely.

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.

9 Useful Tips Working with Operating System using Python

There are several situations when we want to work with the operating system using python. We may want to see the user details or want to do some tasks with the files and directories. If you are a system administrator, you will find it useful to work with Operating System as one can easily automate some repeating tasks of the Operating System using python.

System76 Lemur Pro Review

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.

Top 10 New Features in Linux Kernel 5.8

Linus Torvalds recently announced the release of Linux Kernel 5.8, and he seems delighted with it. He has pointed it out as the most significant release of all time. To developers, this new kernel comes with an addition of 800,000 new code lines and more than 14,000 changed files. To the average user, you might not see many eye-candy changes, as seen in the earlier releases.

How to install Flutter on Linux

Do you want to install Flutter and start creating awesome Android and iOS applications? Then you have come to the right place. Here is a comprehensive tutorial on how to install Flutter on Linux. If you are new to Flutter, don't worry, we got that covered too as we shall go in-depth understanding of Flutter and what it brings to the table.

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

CopyQ – Advanced clipboard manager for Linux

We have all had that moment when we copied a text, but we first needed another one, and in the process, lost the first one. If you're lucky, you can get it back quickly with a bit of work.

5 ways to check if a Port is open on a remote Linux PC

There is an ample number of ways to check for any open ports on a remote Linux PC. Knowing open ports on a Linux machine helps system administrators to connect to the remote PC for troubleshooting system and cloud server issues.

The 6 Best Linux File Recovery Software

Now and then, most of us lose important data in our computers either through accidental deletion, virus attacks, permanent removal of files, etc. Some of these files contain critical information that cannot be assumed and needs recovery. In this post, we will discuss some of the best data recovery available for Linux systems. The fantastic part is that most of them are opensource and freely available for use.

Guide to watching Disney+ on Linux

Disney Plus or Disney+ is an on-demand entertainment streaming service in which you should see the content produced by Disney Studios, Star Wars, Marvel, Pixar, and National Geographic. The service was launched on the 12th of November 2019.