CP (copy an archive or directory)
[Root @ Linux ~] # Cp [-adfilprsu] Destination)
[Root @ Linux ~] # Cp [Options] source1 source2 sourc4 .... Directory
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 effect (destination) already exists, the system will first ask if it is a real action during overwriting!
-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!
Example:
Example 1: Copy. bashrc under the Home Directory to/tmp and rename it bashrc
[Root @ Linux ~] # Cd/tmp
[Root @ Linux TMP] # CP ~ /. Bashrc
[Root @ Linux TMP] # cp-I ~ /. Bashrc
CP: overwrite 'basrhc '? N
# Repeat the two operations. Because bashrc already exists under/tmp, add the-I parameter,
# You will be asked if the user is sure before overwriting! You can press N or Y!
# However, if you do not want to ask, add the-F parameter to forcibly overwrite it!
Example 2: copy/var/log/wtmp to/tmp
[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
# Have you noticed this ?! Without adding any parameters, the owner of the file will change, and the permissions will be changed ~
# This is a very important feature! Be careful! In addition, the time for establishing a file connection is not the same!
# If you want to copy all the features of an archive, add-!
[Root @ Linux TMP] # cp-A/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
# Now! The data features are exactly the same! Really nice ~ This is the feature of-!
Example 3: Copy all contents in the/etc/directory to the/tmp directory.
[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 indicates that directories can be copied. However, permissions on files and directories are changed ~
# Therefore, you can also use CP-A/etc/tmp to issue instructions!
Example 4: Create a symbolic link for bashrc copied in Example 1)
[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-2 root Root 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 creates a "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!
# The similarities and differences between the two links will be introduced in the next chapter!
Example 5: If ~ /. Bashrc/tmp/bashrc is copied.
[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! Pai_^
Example 6: Copy bashrc_slink from example 4 to bashrc_slink_2.
[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 link file, you must use the-D or-a parameter!
Example 7: Copy. bashrc and. bash_history in the home directory to/tmp
[Root @ Linux TMP] # CP ~ /. Bashrc ~ /. Bash_history/tmp
# You can copy multiple data to the same directory at a time!
This CP has many functions, and we often use this command because we are copying some data. In general, if we copy others' data (of course, you must have read permission for this file! When copying _ ^), we always want to copy the data to our own end. Therefore, in The Preset conditions, the CP source file and target file have different permissions, the owner of the target file is usually the instruction operator. For example, in the example 2 above, because I am a root user, the copied file owner and group are changed to the root user! Do you understand this ?! Pai_^
Because of this feature, some special permission files, such as password files (/etc/shadow) and some configuration files, need special attention during Backup, you cannot directly copy files using CP, but you must add parameters such as-A or-P that can completely copy files! 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 still cannot revise your archives! Note!
As for the above example, the fourth example is the most interesting. Using-L and-s will create the so-called link file ), however, these two links do not show the same situation. What is this? That-L is the so-called hard link. As for-s, it is the symbolic link. This is not introduced here, because this involves the knowledge of I-node, which we have not introduced yet, the next chapter will discuss the link issue! In short, because CP has various archive attributes and permissions, you must be aware of the following during replication:
& #8226; do I need to retain the full source file information?
& #8226; is the source file a symbolic link file )?
& #8226; is the source file special, such as FIFO or socket?
& #8226; is the source file a directory?