Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the cp command

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you how to understand the cp command, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can learn something.

Introduction to 01 command

Today we will introduce the replication of files and directories.

Cp-- copies a file or directory

02 order detailed explanation

Grammar

Cp [options] Parameter 1 Parameter 2

This is the syntax of the cp command, and you can see that the cp command must provide two parameters when it is used.

Parameters.

Parameter 1 represents the source file, that is, which file or directory to copy, and parameter 2 represents the target file, that is, which file or directory to copy to.

Cp [options] Source File destination File

Copy the docker.sh, providing only one parameter, and the system prompts you that a target file is missing. After providing two parameters, the docker.sh was successfully copied to docker1.sh.

The cp command has three main modes of operation:

Copy file to file copy to directory copy to directory

Copying docker.sh to docker1.sh is the first mode of operation. When you execute the cp command, if the target file does not exist, the target file is created; if the target file already exists, the target file is overwritten with the source file by default.

First create a new old_file file, and then copy it to the new_file file. The new_file file does not exist before the cp command is executed, so the system generates new_file.

At this time, the new_file file and the old_file file are blank files, and the cat command can be used to view the contents of the file, which will be introduced in the following article.

Then add a sentence to the old_file file, save and then execute the cp command to view the new_file file and find that the contents of the new_file file have been overwritten by the old_file file.

The above is the mode of operation of copying files to files. Next, let's introduce the mode of operation of copying files to directories.

File-to-file and file-to-directory, the biggest difference between the two operation modes is that the file-to-directory mode can contain multiple source files at the same time, while the file-to-file mode allows only one source file, and there is no difference between the other modes.

As you can see, under the operation mode of copying files to directory, the system does not report an error; but in the operation mode of copying files to files, it provides three parameters, and the system reports an error, indicating that new_file is not a directory.

Finally, the operation mode of copying directory to directory is introduced. In this mode, you can copy both files and directories, but when copying directories, you must add the "- r" option before the cp command copies the source directory to the destination directory.

Before adding the "- r" option, the system prompted that the "- r" option was not provided, omitting the copy of the directory, only the docker.sh file was copied.

After adding the "- r" option, the directory was successfully copied to the target directory. You can also use the "- R" option, which has the same effect as the "- r" option.

It is important to note that in the file-to-directory, directory-to-directory mode of operation, the directory specified by the target file must exist, and the cp command does not create a directory for us.

Option

Cp command options a little bit, here I choose a few more commonly used options to introduce to you, the rest of you can consult the relevant materials to learn.

The first is the "- I" option. When executing the cp command, if the destination file exists, the destination file is overwritten with the source file by default. After adding the "- I" option, we will be asked before overwriting.

Cp-I source file target file

First we create a new old_file file, copy it to the new_file file, and then add a sentence to the old_file file.

At this point, new_file has no content, so we execute the cp command and add the "- I" option at this time.

After adding the "- I" option, we will be asked if we want to overwrite the target file. Only when we type "y" (Y, yes, 1, Yes, etc., can also represent "yes" generally, usually use y), the system will be overwritten, and any other characters entered will be considered not to be overwritten.

After adding the "- I" option, we will be asked when the target file exists. If there are a large number of target files, it will take a lot of work for us to enter the answers, especially if we only want to copy the target files that do not exist, this option obviously does not meet our needs.

To do this, Linux provides us with the "- n" option. After adding this option, the system does not overwrite existing target files by default, which can be understood as copying only non-existing target files.

Cp-n source file target file

Create new old_file and new_file files, then modify the old_file file, add the "- n" option when executing the cp command, and view the new_file file, which is still a blank file, indicating that new_file is not overwritten with old_file.

In addition to choosing not to overwrite an existing target file, you can also choose to force a copy of the file, regardless of whether the target file exists or not, through the "- f" option.

Cp-f source file target file

In addition, you can choose to copy only when the time attribute of the source file is newer than that of the target file or when the target file does not exist, which is equivalent to an update through the "- u" option.

For instance. I created a new work Weekly report on my desktop, recording my work this week, and then I want to copy this document to a folder, but last week's work Weekly report already exists in this folder. At this time, I must use the desktop work Weekly report to overwrite the work Weekly report in the folder, on the contrary, if the folder's work Weekly report is newer than the desktop work Weekly report, it will not be overwritten. " The-u "option can perform this work.

Cp-u source file target file

First of all, update the new_file file. At this time, the new_file file is newer than the old_file file. Add the "- u" option to copy it and find that the content of the new_file file has not changed.

The replication of the above options does not contain attributes, such as time, permissions, and so on. If you want to copy the properties of the same source file, you must use the "- p" option.

Cp-p source file target file

First, use the stat command to check the properties of old_file and new_file, and then add the "- p" option to copy. It is found that the access time and modification time of the new_file file are not the current time, but the access time and modification time of the old_file file.

File permissions are also copied to the new_file file. The chmod command is used to modify the permissions of files and directories, which will be explained in more detail in later articles.

03 knowledge summary

Syntax: cp [option] Source file destination file usage: copy file or directory option:-iMuthafure-interaction hint before overwriting-n Mushashi-do not overwrite existing file-fmermerlym-forced copy-uMuMuk-update-pMube-retain the attributes of the source file-r,-Rafael-recursive copy directory parameters: source file, Target file (both absolute address and relative address) other: three operation modes: file to file, file to directory, directory to directory file to file Only allow two parameter directories to the directory mode of operation, must add "- r" or "- R" option file to the directory, directory to directory mode of operation, the directory must exist the above content, that is, how to understand the cp command, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report