"Command CP" linux CP Copy command parameters and usage details

Source: Internet
Author: User

CP (copy files or directories)
[Email protected] ~]# CP [-ADFIlprsu] Source file (source) destination Archive (destination)
[Email protected] ~]# 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 (link file), 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 one: Copy the. BASHRC from the home directory to/TMP and rename it to BASHRC
[Email protected] ~]# cd /tmp
[email protected] tmp]# CP ~/.BASHRC BASHRC
[Email protected] tmp]# cp-i ~/.BASHRC BASHRC
Cp:overWrite ' BASRHC '? N
# Repeat two times, because BASHRC already exists under/TMP, plus the-i parameter,
# You will be asked if the user is sure 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 the/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]# ls -l/var/log/wtmp wtmp
-rw-rw-r–1 root utmp 71808 Jul 12:46/var/log/wtmp
-rw-r–r–1 root root 71808 Jul 21:58 wtmp
# Did you notice?!without any parameters, the owner of the file changes, and even the permissions change.~
# This is a very important feature! Be careful! Also, even the time to set up the file is different!
# If you want to copy all the features of the file together, you can add a-a-oh!
[Email protected] tmp]# cp-a/var/log/wtmp wtmp_2
[Email protected] tmp]# ls-l/var/log/wtmp wtmp_2
-rw-rw-r–1 root utmp 71808 Jul 12:46/var/log/wtmp
-rw-rw-r–1 root utmp 71808 Jul 12:46 wtmp_2
# Know it! The entire data feature is exactly the same sunglass! This is a feature of-a!

Example three: Copy all the contents of the/etc/directory to/tmp
[Email protected] tmp]# cp/etc//tmp
Cp:omitting directory '/etc ' <== if it is a directory, cannot be copied directly, add-r parameter
[Email protected] tmp]# cp-r/etc//tmp
# still want to emphasize again Oh! -R is a directory that can be copied, but the permissions of the file and directory will be changed ~
# So, you can also use Cp-a/etc/tmp to give orders!

example four: Set up a Nexus file (symbolic link) For example one copy of BASHRC
[Email protected] tmp]# ls-l BASHRC
-rw-r–r–1 root root 395 Jul 22:08 BASHRC
[Email protected] tmp]# cp-s BASHRC Bashrc_slink
[Email protected] tmp]# cp-l BASHRC bashrc_hlink
[Email protected] tmp]# ls-l bashrc*
-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
# The Bashrc_slink is caused by the-s parameter, which is created by a "shortcut",
# so you'll see at the far right of the file, it shows 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, but the link number in the second column changes to 2 ~ instead of the original 1!
# The similarities and differences between the two ways of linking, we will introduce in the next chapter!

example five: If ~/.BASHRC is newer than/TMP/BASHRC
[[email protected] Tmp]#&NBSP; cp-u ~/.BASHRC/TMP/BASHRC
# this-u is copied only when the target file differs from the source file.
# So, ! ^_^

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 original copy is a link file, but the actual file of the link is copied over the
# 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 Seven: Copy the. BASHRC and. Bash_history of the home directory to/tmp under
[[email protected] tmp]# CP ~/.BASHRC ~/.bash_ History/tmp
# can copy multiple data to the same directory at once!
This CP has a lot of functions, and because we often do some data copying, we 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?! ^_^

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.


Command CP linux CP Copy command parameters and usage detailed

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.