6 Ways to Use Cp Command in Linux

Hello there, In this guide, we will learn about the cp Command in Linux. Cp stands for the copy. It’s obvious that we use the cp command in Linux for copping the files and directories. But we can use it in various ways. Also, you may get some errors while copying a directory using the cp command. After going through this guide tutorial you will be able to copy files and directories in Ubuntu, Debian, or any other Linux distribution.

cp command in Linux

What does the cp command do? Does it copy the file or the contents of a file?

Well, this very interesting question. Almost every beginner Linux user gets confused about it.

So let’s clear the confusion. We can use cp command for the following file operations-

  1. Copying the contents of a file to another file.
  2. Copying the file from one directory to another directory.
  3. Copying Directories as well as their files from one place to another place.

Copy File Using Cp Command

As we already have mentioned that using the cp command we can copy files and directories in Linux. So, let’s see the syntax for copying files-

cp [options] files

Let’s have a look at the example-

cp myfile.txt  anotherfile.txt

But if you are coming from the Microsoft windows background then you may think- In order to copy files in windows we do not have to give a new name to the copied file but however we can change the name later.

Well, In windows also when you copy a file and paste it into the same folder then we get a warning that the file already exists. But if don’t care and hit enter file gets saved with the same name with postfix.

Like, Index.html gets saved as Index1.html.

Let’s talk about Linux now, When you run cp myfile.txt anotherfile.txt command on your terminal then myfile.txt will be copied & renamed, and saved as anotherfile.txt.

In case, if anotherfile.txt already exists in your system then it will be overwritten and if doesn’t then the new file will be created.

Copy Multiple Files Using Cp Command

We can copy multiple files using the cp command. In order to do so, we have to specify the source file names and destination.

cp [option] [file1 file2 file3 file4] [Directory]

Suppose, you have four files file1.txt, file2.txt, file3.txt, and file4.txt in the "Documents" directory and you want to copy them on your Desktop then you will have to run the following command-

cp file1.txt file2.txt file3.txt file4.txt Desktop

Isn’t it easy?

Copy Directory Using Cp Command in Linux

Copying a directory in Linux using the cp command is as simple as copying files but we need to use -r option. Let’s have a look at the syntax to copy the directory

cp -r Dir1 Dir2

Let’s have an example for a clear understanding-

Suppose, we have a directory “Products” which is available in the Documents directory and we want to copy it on the Desktop. So, we will use the following command-

cp -r Products Desktop

Copy a File From One Directory to Another in Linux

Well, the command is the same, We use the cp command in order to copy a file from one directory to another but we have to specify the path properly.

How do we specify the path?

In the path, we specify the source and destination path. The source path is the place where your file is available and the destination path is the place you want to copy your file.

Let’s check the syntax-

cp [option] [Source Path] [Destination Path]

Suppose, You want to copy “Textfile.txt” from the “etc” directory to your desktop then you will have to run the following command-

cp /etc/Testfile.txt Desktop

Copy All Sub-Directory and Files

Yet we have learned how to copy files or directories using cp command in Linux. But how can we copy all the sub-directory as well as their files from one place to another place in Linux-based operating systems?

Well, as you know in order to copy a directory we have to use the -r option otherwise we get the error-

So the first thing is that we will have to use -r option. And also we may need to use sudo if we are trying to copy a directory that requires permission.

Let’s check the syntax and then example-

cp [option] [source] [destination]

example-

Suppose, we want to copy the “etc” directory to the Desktop. The “etc” directory is the part of Linux file system so we will have to use sudo for copying it.

sudo cp -r /etc/ Desktop

After running the command in the example, the “etc” directory as well as its subdirectories and files will be copied to the Desktop.

If you want to copy it to the current working directory then you can run the following command-

sudo cp -r /etc/ ./

Show Files that are Being Copied

We can use many options with the cp command and one of them is -v option. The -v is one of the cp command options that we can use to see the files that are being copied.

It is very simple to use the -v option. Let’s check the syntax and example-

cp -v [Source Directory /File] [Destination]

Example-

cp -v /etc/MyFile.txt Desktop

Here, In this example, we are copying MyFile.txt from the “etc” directory to our desktop. If you get any error then place “sudo” before the command.

Conclusion

The cp command is used for copying things (here things mean files and directories) in Linux. But sometimes you may need proper knowledge to use it otherwise you may get errors. So, In this guide, we have covered the cp command and its useful options so that you do not face any difficulty while using the command.

Leave a Comment