Home  >  Article  >  Operation and Maintenance  >  How to copy files using cp command in linux

How to copy files using cp command in linux

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-14 16:02:0023502browse

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

How to copy files using cp command in linux

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

cp command format

How to copy files using cp command in linux

##Take moving file 1234.txt as an example

ls -l      //显示当前文件夹下所有文件的信息

How to copy files using cp command in linux

Copy 1234.txt file to new file 2345.txt

copy 1234.txt 2345.txt

Here you can see that the two files have the same size but different timestamps

How to copy files using cp command in linux

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

How to copy files using cp command in linux

What happens if the new file name after copying already exists?

After writing some content to 1234.txt and copying it to 2345.txt again, you will find that the original 2345.txt file will be overwritten and there will be no reminder and the execution will be successful.

How to copy files using cp command in linux

To avoid not knowing whether a file with the same name has been overwritten, you can add the -i option

Example:

cp -i 1234.txt 3456.txt

You can see when the new When the file name already exists in the current directory, there will be an overwrite confirmation reminder.

Enter n to cancel and continue copying.

How to copy files using cp command in linux

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

cp 123.txt test        //同目录
cp 1234.txt /home/zinkky/test      //不同目录时需要带上路径

How to copy files using cp command in linux

Copy directories to The new directory needs to add the option -r

Example:

cp -r test python

How to copy files using cp command in linux

(recommended learning:

linux tutorial)

The above is the detailed content of How to copy files using cp command in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn