Cp (copy an archive or directory)
[Root @ linux ~] # Cp [-adfilprsu] destination)
[Root @ linux ~] # Cp [options] source1 source2 soure3... directory
Parameters:
-A: This option is usually used when copying directories. It retains links and file attributes, and Recursively copies directories. Its role is equal to the combination of dpR options.
-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 12:46 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 395 Jul 18 22:08 bashrc
-Rw-r -- 2 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:
• Do I need to retain the full source file information?
• Is the source file symbolic link file )?
• Is the source file special, such as FIFO or socket?
• Is the source file a directory?