Get the App
SLTechnology News&Howtos  ›  Servers  › 

The method of copying Files by cp Command in linux

Shulou Source: shulou.com Published: 2022-06-01 21:47:56 09月20日 Update

This article will explain in detail the method of copying files with cp commands in linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

In linux, you can use the cp command to copy a file and copy a source file or directory to a specified destination file or directory; it can copy a single source file into a specific file with a specified file name or an existing directory; this command supports copying multiple files at the same time.

The operating environment of this tutorial: centos7 system, thinkpad T480 computer.

Command format of cp

Take moving file 1234.txt as an example

Ls-l / / displays information about all files in the current folder

Copy the 1234.txt file to the new file 2345.txt

Copy 1234.txt 2345.txt

Here you can see that the two files are of the same size and have different timestamps

Copy the 1234.txt file to the new file 3456.txt and keep the timestamp the same

What if the copied new file name already exists?

If you write something to 1234.txt and copy it to 2345.txt again, you will find that the original 2345.txt file will be overwritten and will be executed successfully without warning.

To avoid not knowing whether a file with the same name is overwritten, you can add the-I option.

Example:

Cp-I 1234.txt 3456.txt

You can see that there is an overwrite confirmation reminder when the new file name already exists in the current directory

Enter n to cancel the replication.

Copy files to folders in the same directory and other folders in different directories

Cp 123.txt test / / same directory cp 1234.txt / home/zinkky/test / / different directories need to be taken with a path

Copying a directory to a new directory requires the option-r

Example:

Cp-r test python

This is the end of this article on "the method of cp command to copy files in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: File directory command different file name folder article method same content time more source file good practical successful two information again single Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MySQL Microsoft Shulou Technology MariaDB