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 use the copy command under linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the knowledge of "how to use the copy command under linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The copy command under linux is "cp". Cp command is mainly used to copy files and directories, the syntax "cp [option] source file target file", you can copy one or more source files or directories to the specified file or directory; at the same time, with some options, you can also copy the entire directory, and compare the new and old of the two files to upgrade and other functions.

The operating environment of this tutorial: CentOS 6 system, Dell G3 computer.

The copy command under linux is "cp".

The full name of cp is "copy file" in English, which means copying files in Chinese.

Cp command, mainly used to copy files and directories, can copy one or more source files or directories to a specified destination file or directory

The basic format of the cp command is as follows:

Cp [options] Source File destination File

Options:

-a: equivalent to the collection of-d,-p,-r options, which we will introduce one by one

-d: if the source file is a soft link (not valid for hard links), the copied target file is also a soft link

-I: ask, if the target file already exists, whether to overwrite

-l: establish the target file as a hard-linked file of the source file instead of copying the source file

-s: establish the target file as a soft link file for the source file instead of copying the source file

-p: the copied destination file retains the attributes of the source file (including owner, group, permissions and time)

-r: recursive replication, used to copy directories

-u: if the destination file is different from the source file, use this option to update the target file, which can be used to upgrade and standby the file.

It is important to note that there can be multiple source files, but in this case, the target file must be a directory.

Description: cp command with the help of some options, you can also copy the entire directory, and compare the old and new files to upgrade and other functions.

Examples of the use of the cp command:

[example 1] basic usage of cp command

The cp command can copy both files and directories. Let's first look at how to copy files, such as:

[root@localhost ~] # touch cangls# establish the source file [root@localhost ~] # cp cangls / tmp/# copy the source file to the / tmp/ directory without changing its name

If you need to rename replication, the command is as follows:

[root@localhost ~] # cp cangls / tmp/bols# renamed replication

If a file with the same name already exists in the copied destination, you will be prompted whether to overwrite it, because the cp command executes the alias of "cp-I" by default, for example:

[root@localhost ~] # cp cangls / tmp/cp: whether to overwrite "/ tmp/cangls"? there is a file with the same name in the target location of y #, so you will be prompted whether to overwrite it.

Let's take a look at how to copy a directory, but you only need to use the "- r" option to copy a directory, for example:

[root@localhost ~] # mkdir movie# set up a test directory [root@localhost ~] # cp-r / root/movie/ / tmp/# directory original name copy

[example 2] copy soft link file

If the source file is not an ordinary file, but a soft link file, can you copy the properties of the soft link? Let's try:

[root@localhost] # ln-s / root/cangls / tmp/cangls_slink# set up a test soft link file / tmp/cangls_ Slink [root @ localhost ~] # ll / tmp/cangls_slinklrwxrwxrwx 1 root root December 14 05:53 / tmp/cangls_slink-> / root/cangls# source text itself is a soft link file [root@localhost] # cp / tmp/cangls_slink / tmp/cangls_t1# copy soft link file However, without adding the "- d" option [root@localhost ~] # cp-d / tmp/cangls_slink / tmp/cangls_t2# to copy the soft link file, and adding the "- d" option [root@localhost ~] # ll / tmp/cangls_t1 / tmp/cangls_t2-rw-r--r-- 1 root root 0 June 14 05:56 / tmp/cangls_t1#, you will find that the source file of the soft link is actually copied without the "- d" option. Instead of the soft link file lrwxrwxrwx 1 root root December 14 05:56/tmp/ cangls_t2- > / root/cangls#, but if the "- d" option is added, the soft link file will be copied

This example shows that if you do not use the "- d" option when copying a soft-link file, the cp command copies the source file, not the soft-link file; only if the "- d" option is added will the soft-link file be copied. Please note that the "- d" option is not valid for hard links.

[example 3] preserve source file attribute replication

We found that after the copy command was executed, the time of the destination file became the execution time of the copy command, not the time of the source file. For example:

[root@localhost ~] # cp / var/lib/mlocate/mlocate.db / tmp/ [root@localhost ~] # ll / var/lib/mlocate/mlocate.db-rw-r-1 root slocate2328027 June 14 02:08/var/lib/mlocate/mlocate.db# pay attention to the time of the source file and the group to which it belongs [root@localhost ~] # ll / tmp/mlocate.db-rw-r- 1 root root2328027 June 14 06:05/tmp/mlocate.db# due to replication command executed by root user So the group of the target file is root, and the time becomes the execution time of the copy command.

When we perform backups and log backups, the time of these files may be an important parameter, which requires the "- p" option. This option preserves the properties of the source file, including the owner, group, and time. For example:

[root@localhost ~] # cp-p / var/lib/mlocate/mlocate.db / tmp/mlocate.db_2# using the "- p" option [root@localhost ~] # ll / var/lib/mlocate/mlocate.db / tmp/mlocate.db_2-rw-r- root slocate 2328027 June 14 02:08 / tmp/mlocate.db_2-rw-r- root slocate 2328027 June 14 02:08 / var/lib/mlocate/mlocate.db# source text All attributes of the item and the target file are the same. Including time

As we said before, the "- a" option is equivalent to the "- d,-p,-r" option, which we have talked about separately. So, when we use the "- a" option, all the attributes of the target file and the source file are the same, including the owner of the source file, group, time, and soft linking. It is more convenient to use the "- a" option instead of the "- d,-p,-r" option.

[example 4] "- l" and "- s" options

If we use the "- l" option, the target file will be established as a hard link to the source file; if we use the "- s" option, the target file will be established as a soft link to the source file.

These two options are different from the "- d" option, which requires that the source file must be a soft link before the target file can be copied as a soft link, while the source file of the "- l" and "- s" options only needs to be a normal file. the target file can be directly copied as a hard link and a soft link. For example:

[root@localhost ~] # touch bols# set up test file [root@localhost ~] # ll-I bols262154-rw-r--r-- 1 root root 0 June 14 06:26 bols# source file is just a normal file Instead of the soft link file [root@localhost ~] # cp-l / root/bols / tmp/bols_ h [root @ localhost ~] # cp-s / root/bols / tmp/bols_s# use "- l" and "- s" options to copy [root@localhost ~] # ll-I / tmp/bols_h / tmp/bols_s262154-rw-r--r-- 2root root 14 / 06 06:26/tmp/bols_h# target file / tmp/ Hard link file with bols_h as source file 932113 lrwxrwxrwx 1 root root October 14 06:27/tmp/bols_s-> / root/bols# target file / tmp/bols_s soft link file "how to use copy commands under linux" is introduced here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Servers

Wechat

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

12
Report