CP
[Root@linux ~]#CP[-ADFIlprsu] Source file (source) destination Archive (destination)
[Root@linux ~]#CP[Options] Source1 source2 source3 .... directory
Parameters:
-A: equivalent to the meaning of-pdr;
-D: If the source file is a property of the linked file (linkfile), the link file attribute is copied rather than the file itself;
-F: For force, if there is any duplication or other doubt, the user will not be asked, but forced to copy;
-I: If the destination file (destination) already exists, the overwrite will ask if the action is true!
-L: The link file for the hard link is established, not the copy file itself;
-P: Copy the past together with the attributes of the archive, rather than using the default attributes;
-R: Recursive continuous replication, used for directory replication behavior;
-S: Copy as a symbolic link file (symbolic link), i.e. "shortcut" files;
-U: Update destination If destination is older than source!
Finally, note that if the source file has more than two, then the last destination file must be "directory"!
Example:
Example: Copy the. BASHRC from the home directory to/TMP and rename it to BASHRC
[root@linux ~]# CD /tmp
[Root@linux tmp]# cp ~/.BASHRC BASHRC
[Root@linux tmp]# cp-i ~/.BASHRC BASHRC
Cp:overwrite ' BASRHC '? n
# Repeat two times, because the BASHRC already exists in/TMP, plus the-i parameter,
# will ask the user if it is OK before overwriting! You can press N or y!  
# However, conversely, if you do not want to ask, then add-f This parameter to force direct overwrite!
Example two: Copy/var/log/wtmp to/tmp
[[email protected] tmp]# cp/var/log/wtmp. <== want to copy to the current directory, the last. Don't forget
[[email protected] tmp]#
Example Three: Copy all contents of/etc/to/tmp under
[[email protected] tmp]# cp/etc//tmp &NBSP;
Cp:omitting D Irectory '/etc ' <== if it is a directory, cannot be copied directly, add-r parameters
[[email protected] tmp]# cp-r/etc//tmp &NBS P The
# still needs to be stressed again! -R is a directory that can be copied, however, the permissions of the file and directory will be changed ~
# So you can also use Cp-a/etc/tmp to give orders!
Example four: Create a link file (symbolic link)
[[email protected] tmp]# ls-l BASHRC &NBSP For example one copy BASHRC;
-rw-r –r–1 root root 395 Jul 22:08 bashrc
[[email protected] tmp]# cp-s BASHRC bashrc_slink &N Bsp
[[email protected] tmp]# cp-l bashrc bashrc_hlink
[[email protected] tmp]# ls-l bashrc* & nbsp
-rw-r–r–2 root root 395 Jul 22:08 bashrc
-rw-r–r–2 root root 395 Jul 22:08 Bashrc_hlink
lrwxrwxrwx 1 root root 6 Jul 22:31 bashrc_slink, BASHRC
# that Bashrc_slink is by-s The parameters are created by a "shortcut",
# so you'll see at the far right of the file where the file is "connected" to!
# As for that Bashrc_hlink fun! After setting up this file, BASHRC and Bashrc_hlink
# All parameters are the same, except that the link number in the second column changes to 2 ~ instead of the original 1 Oh!
# The similarities and differences between these two ways of linking, we will introduce in the next chapter!
Example five: If ~/.BASHRC is newer than/TMP/BASHRC, copy it.
[Email protected] tmp]# cp-u ~/.BASHRC/TMP/BASHRC
# this-u feature is only copied when the target file differs from the source file.
# So, more often used in "Backup" work! ^_^
Example six: Copy the bashrc_slink caused by example four into bashrc_slink_2
[email protected] tmp]# CP Bashrc_slink bashrc_slink_2
[Email protected] tmp]# ls-l bashrc_slink*
lrwxrwxrwx 1 root root 6 Jul 22:31 Bashrc_slink-BASHRC
-rw-r–r–1 root root 395 Jul 22:48 bashrc_slink_2
# This example is also very interesting! The link file was originally copied, but the actual file of the link was copied over.
# that is, if you do not add any parameters, the copy is the source file, not the properties of the linked file!
# to copy the properties of a linked file, you have to use the-D or-a parameter!
Example VII: Copy the home directory's. BASHRC and. Bash_history to/tmp
[email protected] tmp]# CP ~/.BASHRC ~/.bash_history/tmp
# Multiple data can be copied to the same directory at once!
This CP has a lot of functions, and since we are often doing some data replication, we will often use this command. In general, if we go to copy other people's data (of course, the file you have to have Read permission to do!) ^_^), we always want to copy the data to the end of our own, so, in the pre-condition, CP source file and destination file permissions are different, the owner of the destination file is usually the instruction operator itself. For example, in the above example two, because I am the identity of root, so the copy of the file owner and the group has changed to become root! Say so, can you understand?! ^_^
Because of this feature, when we are doing backup, some special privilege files that require special attention, such as password file (/etc/shadow) and some configuration files, can not be copied directly with CP, but must be added with a or-p and so on can fully copy the file permissions parameters! In addition, if you want to copy files to other users, you must also pay attention to the permissions of the file (including read, write, execute and file owner, etc.), otherwise, other people will not be able to make changes to the files you have given the action Oh! Pay attention!
As for the example above, the fourth example is the most interesting, and using-l and-s will create a so-called link file, but the two types of links do not show the same situation. What the hell is this? That-L is the so-called hard link, as for-S is symbolic link, bird brother Here is not introduced, because this involves i-node related knowledge, we have not introduced, the next chapter to discuss this link problem Oh! In short, because CP has various attributes of the file properties and permissions, so in the copy, you have to understand clearly:
? Is it necessary to complete the retention of the source file information?
? is the source file a Nexus file (symbolic link file)?
? is the source file a special file, such as FIFO, socket, etc.?
? is the source file a directory?
It should be explained that in order to prevent the user inadvertently using the CP command to destroy another file, such as the user specified target file name already exists, the CP command after copying the file, the file will be overwritten by the Xinyuan file, it is recommended that users use the CP command to copy files, it is best to use the I option.
Linux knowledge points, a day one of the CP