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,648FansLike
360FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to install CMake on Ubuntu

CMake is a cross-platform free and open-source software tool designed to build, test, and package the software. CMake uses a simple platform and compiler-independent configuration files to control the software compilation process.

How to install Lightworks on Ubuntu

Even though Linux may not get a native installer of video editing software like Adobe Premiere or Final Cut Pro, that doesn't mean there are no industry standards tools available. Lightworks is non-linear editing (NLE) video mastering app for Windows, Linux, and macOS. Installing it on Ubuntu is simple due to deb package availability.

How to install DaVinci Resolve on Fedora

Davinci Resolve is a professional application used for color correction, video editing, visual effects, and motion graphics. It is one of the extensively used software by movie industries located in Hollywood.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.

5 Ways to Open a Terminal in Ubuntu

Even though Ubuntu supports many applications with amazing Graphical User Interfaces (GUI), there are always reasons why users prefer using the Terminal to perform different tasks.

How to install Wine on Fedora Workstation

Linux distributions are becoming more and more popular every day, and Fedora Workstation is not left behind. This popularity brings forth the need to run Windows applications on Linux distros like Fedora. Windows has quite some excellent Software that is not available for Linux.

MUST READ

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.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

15 Tar command in Linux uses with examples

Tar is a famous utility that is basically used for collecting multiple files in a single archive. This file is often called a 'tarball'. Today, we are going to tell you about the different things you can do with tarballs using the tar command, with practical examples.

6 Best Linux Distributions for Beginners

Making the shift to Linux is not as complicated as some people make it out to be. There is a variety of Linux distributions available that cater to the needs of people who are planning on making the jump to Linux from any other operating system.

Top 5 reasons to switch from Windows to Linux right now

Do you love Windows or Linux? Being on a Linux website, it's not hard to take a guess, but what are the strong reasons to switch to Linux? Linux is a 100% free OS. Is that reason alone enough? Let's find out! Never have there been more compelling reasons to make the switch. Here are just five great reasons for users to create that magical move from the wayward world of Windows to the lauded land of Linux.

All about Ubuntu editions and which version should you use?

Ubuntu is one of the most popular Linux distributions developed and released by Canonical, and not without reason. It has very enriched repositories, with support for all the programs you could ever need.