Home >Operation and Maintenance >Linux Operation and Maintenance >Why does the cp command not overwrite files in Linux?

Why does the cp command not overwrite files in Linux?

WBOY
WBOYOriginal
2022-04-12 10:19:5410751browse

In Linux, you can use the "-i" parameter of the cp command to achieve non-overwriting of files. The cp command is used to copy files or directories. When the parameter is set to "-i", it will be given before overwriting the file. A prompt will appear. Answer "n" to indicate that you do not agree to overwrite the file. Answer "y" to indicate that you agree to overwrite the file. The syntax is "cp -i file name".

Why does the cp command not overwrite files in Linux?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to not overwrite files with the cp command in Linux

The Linux cp (full English spelling: copy file) command is mainly used to copy files or directories.

Syntax

cp [options] source dest

or

cp [options] source... directory

Parameter description:

-a: This option is usually used when copying a directory. It retains links and file attributes. and copies everything under the directory. Its effect is equal to the dpR parameter combination.

-d: Keep the link when copying. The links mentioned here are equivalent to shortcuts in Windows systems.

-f: Overwrite an existing target file without giving a prompt.

-i: Contrary to the -f option, a prompt is given before overwriting the target file, asking the user to confirm whether to overwrite. The target file will be overwritten when answering y.

Use awk loop to give Yes (y) or No (n), agree to overwrite or disagree to override. Obviously agreeing to cover is a bit useless, you can use -f to force coverage.

The example is as follows:

Copy all files and subdirectories under the folder Raw to the New directory

awk 'BEGIN {cmd="cp -ri ./Raw/* ./New "; print "n" |cmd; }'

Remarks: print "n" | cmd, print first cmd command and then print n.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of Why does the cp command not overwrite files 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