@ "\" Indicates the role of the symbol in CP @

Source: Internet
Author: User

I don't know if you have seen "\" in front of the CP command line before. I am using it for the first time. Now let's take a look at it ~

[Oracle @ test233 ~] $ Cd zy/
[Oracle @ test233 ZY] $ CP ~ /. Bash_profile
[Oracle @ test233 ZY] $ CP-I ~ /. Bash_profile
CP: overwrite "bash_profile "? N

We all know that if you don't want a prompt, enter the following command:

[Oracle @ test233 ZY] $ CP-f ~ /. Bash_profile

Is there any other way?

At this time, we are about to answer the question, and our "\" should also be unveiled ~

\ CP-r-f Source Target
By default, the CP command does not prompt overwrite? But the-I option of CP will prompt, while the Linux Startup File ~ /. Bashrc will name CP
Alias CP = 'cp-I'
In this way, in Linux, the CP command is actually run CP-I, and the "\" symbol is added to make the CP Command run without the alias (CP-I.
Will there be no overwrite? Prompt

Next, let's summarize the CP parameters ~

-A: equivalent to-PDR;
-D:If the source file is a link file, copy the link file property instead of the file itself;
-F: force. If there are repeated or other questions, the user is not asked, but the user is forced to copy;
-I: If the destination file (destination) already exists, the system will first ask if the target file is actually being overwritten!
-L: Create a hard link instead of copying the file;
-P: copy the file together with its attributes, instead of using the predefined attributes;
-R: recursive continuous replication for directory replication;
-S: copy it into a symbolic link, that is, a "shortcut" file;
-U: If destination is earlier than source, it will update destination!
Note that if there are more than two source files, the last target file must be a "directory!

Instance 1:-

[Root @ Linux TMP] # cp-
/Var/log/wtmp wtmp_2
[Root @ Linux TMP] # ls-L/var/log/wtmp wtmp_2
-RW-r-1 root utmp 71808 Jul 18 12:46/var/log/wtmp
-RW-r-1 root utmp 71808 Jul 18 wtmp_2

If-A is not added, the file attributes will not be the same.

[Root @ Linux TMP] # cp/var/log/wtmp. <= If you want to copy data to the current directory, do not forget the last one.
[Root @ Linux TMP] # ls-L/var/log/wtmp
-RW-r-1 root utmp 71808 Jul 18 12:46/var/log/wtmp
-RW-r-1 Root 71808 Jul 18 21:58 wtmp

Example 2: Role of-R

[Root @ Linux TMP] # cp/etc/tmp
CP: omitting directory '/etc' <= if it is a directory, you cannot copy it directly. Add the-R parameter.
[Root @ Linux TMP] # cp-r/etc/tmp
# I want to emphasize it again! -R can copy directories, but files and directoriesPermissionWill be changed ~
# Therefore, you can also use CP-A/etc/tmp to issue instructions!

Example 3: Functions of-L and-S

[Root @ Linux TMP] # ls-l bashrc
-RW-r-1 Root 395 Jul 18 22:08 bashrc
[Root @ Linux TMP] # cp-s bashrc bashrc_slink
[Root @ Linux TMP] # cp-l bashrc bashrc_hlink
[Root @ Linux TMP] # ls-l bashrc *
-RW-r-2 root Root 395 Jul 18 22:08 bashrc
-RW-r-2Root 395 Jul 18 22:08 bashrc_hlink
Lrwxrwxrwx 1 Root 6 Jul 18 bashrc_slink-> bashrc
# The bashrc_slink is caused by the-S parameter and is created 『Shortcut』,
# So you will see at the far right of the file, where the file is connected!
# The bashrc_hlink is interesting! After this file is created, bashrc and bashrc_hlink
# All parameters are the same, but the number of links in the second column changes to 2 ~ Instead of the original 1 Oh!

Example 4: Role of-u

[Root @ Linux TMP] # cp-U~ /. Bashrc/tmp/bashrc
# This-u feature is copied only when the target file is different from the source file.
# So, it is often used in "backup" work!

Instance 5: Copy link files

[Root @ Linux TMP] # cp bashrc_slink bashrc_slink_2
[Root @ Linux TMP] # ls-l bashrc_slink *
Lrwxrwxrwx 1 Root 6 Jul 18 bashrc_slink-> bashrc
-RW-r-1 Root 395 Jul 18 22:48 bashrc_slink_2
# This example is also very interesting! The original link file was copied, but the actual file of the link file was copied.
# That is, if no parameter is added, the source file is copied, not the link file attribute!
# To copy the attributes of a linked file, you must use-D or-.
Parameter!

Example 6: CP does not overwrite the target file

Shell> awk 'in in {cmd = "CP-I A1 A2"; print "N" | cmd ;}'
Note: A1 is the original folder and A2 is the target folder.
Because of this feature, some special permission files, such as password files (/etc/shadow) and some configuration files, need special attention during Backup, instead of using CP for replication, you must add-A or-P
And so on! In addition, if you want to copy an archive to other users, you must also note the permissions of the archive (including read, write, execute, and owner). Otherwise, other people are still unable to revise your archives ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.