Why does the cp command not overwrite files in Linux?

WBOY
Release: 2022-04-12 10:19:54
Original
10680 people have browsed it

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
Copy after login

or

cp [options] source... directory
Copy after login

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; }'
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template