A cp is a command-line utility to copy the files along with their content from one location to another in Linux distributions. Both the files, as well as directories, can be copied using the cp command. LinuxMint is a distribution of Linux operating systems that will be used in this write-up to explain how to use the cp command in Linux.

What is the general syntax of using the cp command in Linux

It has been discussed that the cp command is used to copy the files and directories from one location to another in Linux. So we will need paths of two different locations, one from where the file is being copied and the other, where it has to be pasted. The general syntax of using the cp command will be:

$ cp [options] [source of file/directory where it is being copied] [destination of file/directory where it has to be pasted]

The general syntax is simple to understand, with the use of cp command, we can also use different options of copy, some of them are:

Options Description
-b This option will make the backup of all the contents of the file
-f This option is used to forcly copied all the contents of the file from source to destination
-i This option will confirm from you before copying the files by displaying a message
-n It will not overwrite the file if any file is already present in the destination path

How to use the cp command in Linux

We will explore the usage of the cp command in Linux by using different examples. If we want to copy and paste the file within the same directory, we can use the cp command:

$ cp myfile.txt myNewFile.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/How-to-Use-the-cp-Command-in-Linux-1.png" data-lazy- height="103" src="data:image/svg xml,” width=”753″>

To confirm the new file has been created, we will list down the contents of the directory using the ls command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/How-to-Use-the-cp-Command-in-Linux-2.png" data-lazy- height="163" src="data:image/svg xml,” width=”646″>

We will use the “-i” option so that if the file is already present with the new name, it confirms from us to overwrite it or not:

$ cp -i myfile.txt myFile.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/How-to-Use-the-cp-Command-in-Linux-3.png" data-lazy- height="140" src="data:image/svg xml,” width=”754″>

In the above output, we don’t want to overwrite, so we type “n”. To copy the file, myfile.txt from home directory to Documents directory, we will use the cp command as:

$ cp -v myfile.txt myfile1.txt myNewFile.txt Documents

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/How-to-Use-the-cp-Command-in-Linux-4.png" data-lazy- height="187" src="data:image/svg xml,” width=”789″>

We have used the “-v” option as it will tell us the details of the execution of the command and to confirm that the file has been copied, we run the command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/How-to-Use-the-cp-Command-in-Linux-5.png" data-lazy- height="105" src="data:image/svg xml,” width=”571″>

To create the backup of the myfile.txt, we will use the “-b” option:

$ cp -b myfile.txt backupFile.txt

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/How-to-Use-the-cp-Command-in-Linux-6.png" data-lazy- height="79" src="data:image/svg xml,” width=”765″>

We can also copy the directory with the help of cp command, for example, we will copy the directory “myDirectory” to Documents using the command:

$ cp -r myDirectory Documents

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/How-to-Use-the-cp-Command-in-Linux-7.png" data-lazy- height="78" src="data:image/svg xml,” width=”734″>

We have used the “-r” option so if there is any directory inside the “myDirectory”, it will also be copied.

Conclusion

The cp command is used to copy the files and directories with all of their contents from one location to another. In this write-up, we have explored the utilization of the cp command-line utility with its basic options of backup, verbose, and interactive.