How to Use cp Command on Linux


Copying files is a fundamental task that anyone using a computer performs most frequently. Whether you are a system administrator managing different servers or a student gathering materials, the ability to duplicate files is a useful skill. For Linux users, this capability is empowered by cp command that allows them the ability to copy the files from one directory to another directory. It includes a range of options that help them quickly copy the files according to their desired needs.

Follow this tutorial to learn about:

Syntax of cp Command

The basic syntax to use the cp command to copy a file in a Linux system is given below:

cp source_file destination

Here, the cp command copies the file from the current directory to a destination that could be any directory you choose. You can also use the full path for the source file and the destination with the cp command for copying the files.

How to Use cp Command on Linux

The cp command in the Linux system is a versatile tool that allows you to perform multiple operations, such as:

How to Use cp Command on Linux to Copy the Content of One File to Another File

One of the basic functions of the cp command on Linux is that you can use it to copy the content of one file into another file. It is up to you whether to specify the name of the second file or not, in both cases, the content of a file will be copied to the destination file.

For example, we have two files on the Linux system named file1.txt and file2.txt. The file1.txt includes the text “Hello LinuxGenie Users”, while file2.txt is empty. To copy the content of file1.txt into file2.txt, we can use the following cp command:

cp file1.txt file2.txt

Note: Don’t forget to replace the file names in case of different files.

When you run the above command, the text inside file1.txt will be copied to file2.txt. We can use the cat command with the file name to retrieve the information of the text inside the file for confirmation.

How to Use cp Command on Linux to Copy Files to a Directory

You can also use the cp command on Linux to copy a single or multiple files to another directory. It is possible by using the cp command with the names of the files and the destination directory.

To copy a single file to a destination directory in Linux, you can simply use the following syntax:

cp filename.txt destination

Note: If you run the above cp command again, it will overwrite the already existing file on your Linux system.

For copying multiple files to a destination directory, use the cp command, followed by the file names separated by a space and destination directory in the last.

The syntax to copy two multiple files to a destination directory using the cp command is given below:

cp file1name.txt filen2name.txt destination

Here, we have copied two files, file1.txt and file2.txt to a Downloads directory.

To verify files are copied correctly, you can navigate to the directory using the cd command and use the ls command to view the content of the directory.

If you have multiple files you want to copy to another directory, use the * wildcard option with the cp command. The advantage of using the cp command with the wildcard option is that you don’t need to specify file names with it separately. Instead, simply use the * wild card option with the file format to copy all the files of the specified format to the destination directory.

The cp command syntax to copy the files of .txt format to a destination directory is provided below:

cp *.txt destination

Here, in the following output, we have transferred the text files to a Desktop destination.

Besides that, you can also use the -i flag with the cp command for interactive copying. The advantage of using the -i flag is that the system will warn you about overwriting the file. You can accept the file overwriting by using the y argument, or use n to exit the overwriting.

In the following command, we use the cp command with the -i flag for overwriting the text in file2.txt from file1.txt:

cp -i file1.txt file2.txt

Then verify the content inside the files using the cat command:

How to Use cp Command on Linux to Copy Content of One Directory to Another Directory

Besides specifying the file name or file location with the cp command, you can also provide the directory name to copy the content of that directory to another directory. It can be done by using the cp command with the -R flag followed by the source and destination directory separated by a space. The syntax for copying the content of one directory to another directory using the cp command is provided below:

cp -R source destination

Here, we have used the cp command to copy the content of directory-named files to the destination Desktop:

How to Use cp Command on Linux to Create a File Backup

You can also use the cp command on Linux to create a backup of your file. It can be done by using the –backup argument with the cp command, followed by the filename you want to create backup. The advantage of using this method is that it won’t overwrite the file that already exists in the directory; instead, creates a new file with the same name.

The syntax for creating the file backup on Linux is provided below:

cp --backup source_filename backup_filename

Here, replace the source_filename with the filename of the source text file, while backup_filename needs to be set on your own. As an example, we have created the backup of text file file1.txt with the name file2.txt.

The file2.txt is already present at the default location, however, instead of overwriting it, the system creates a separate file with file2.txt.

The first one in the highlighted box is the original file that is already present, while the next file with the same name is the file backup that is created through the cp command.

If you need more details about using different flags with the cp command in Linux, you can open its manual on the terminal using:

man cp

Conclusion

The cp command is a versatile command on Linux systems used for copying files from one directory to another directory. This article discussed the use of cp commands on Linux in different ways. These include copying the content of one file to another file, copying files to a directory, copying the directory content, or creating a file backup. Within these methods, we have used different flags to perform a specific copy operation. Through this guide, you will quickly learn the use of cp commands and perform copy-related tasks directly from the terminal.

Print Friendly, PDF & Email