In Linux, files can be created either to store some text-based information or to store some programming code of different web applications. Most of the time, we want to make the backup of the files for which some copies are made and stored at different computer locations. Linux is the command-line operating system which means there should be a command to copy files.
In Linux, the cp command is used to make a copy of files, and in this blog, a detailed demonstration of the cp command in Linux will be explained with the help of its examples. This blog will explain the following sections in detail:
- What is the cp Command Utility in Linux?
- What is the Usage of the cp Command Utility in Linux?
Let’s start the guide!
What is the cp Command Utility in Linux?
The cp command is used to copy directories and files from one directory of Linux to the other directory and also, can do both, copying files to the directory as well from the directory. To use the cp command utility in Linux, follow the general syntax:
$ cp [Option] [Source] [Directory]
To understand the usage of the cp command, we will perform some examples in the next sections.
Different options can be used with the cp command, and the usage of these options is explained in the table below:
|–attributes-only||It will not copy the data of the files but only copy its attributes|
|–backup||It will make the backup of the file to be copied at its destination|
|-f||It will forcibly copy the file|
|-i||It will ask the user for confirmation before copying it|
|-s||It will create the symbolic links of the file|
|-v||It will show the progress of the copied files|
There are many other options which can be displayed using the command:
$ cp --help
Let’s now discuss some of the basic usages of the cp command in Linux.
What is the Usage of the cp Command Utility in Linux?
As discussed earlier, the cp command has a key role in managing files and directories in Linux. This section enlists a few of the most used cp commands usages in Linux. Let’s start:
Example 1: How to Copy a File in Linux?
To copy files using the cp command in Linux, first, make a file with the touch command:
$ touch testfile1
After creating the “testfile”, confirm the execution of the above command using the ls command:
Now, to copy this file from the home directory to the “Downloads” directory with the “cp” command:
$ cp /home/itslinux/testfile1 /home/itslinux/Documents/
To verify the execution of the above command, list down the “Documents” directory using the command:
$ ls /home/itslinux/Documents/
The file has successfully copied from the home directory to the Documents directory.
Example 2: How to Copy Multiple Files in Linux?
We can copy multiple files using the cp command to some other directory. For example, “myfile1”, “myfile2”, and “myfile3” are copied from the home directory to “/home/itslinux/Documents/” using the cp command:
$ cp -v myfile1 myfile2 myfile3 /home/itslinux/Documents/
All the files are copied to the destination directory.
Example 3: How to Copy One Directory in Linux?
We can also use the cp command to copy the directory from one source directory to some destination directory. For example, we have a “mydirectory1” at home directory, which is copied to “/home/itslinux/Documents/” using the cp command with its “-r” option:
$ cp -vr mydirectory1 /home/itslinux/Docuements/
The directory has been copied as displayed on the screen.
Example 4: How to Copy Multiple Directories in Linux?
Like the multiple files, we can also copy multiple directories to the other destination directory. For example, mydirectory2, mydirectory3, and mydirectory4 have been copied to “/home/itslinux/Downloads/” using the cp command:
$ cp -vr mydirectory2 mydirectory3 mydirectory4 /home/itslinux/Downloads/
All the directories are being copied.
That’s all from this post!
In Linux, the cp command utility is used to copy different files/directories with different options. Using the cp command, a single, as well as multiple files/directories, can be copied. This post has briefly explained the working and usage of the cp command in Linux. For a better understanding of the readers, different examples are used to explain the usage of the cp command in various scenarios.