Linux CP command parameters and usage details---linux copy file command CP

Source: Internet
Author: User

linux CP command parameters and usage details---linux copy file command CP

[[Email protected]linux ~]# cp [-adf ILPR su ] source file (source) destination archive (destination)
[[Email protected]linux ~]# CP [options] Source1 Source2 Source3. Directory
parameter:
-A: equivalent to-pdr meaning;
-D : If the source file is the attribute of the linked file (link file ), the link file property is copied instead of the file itself;
-F: For force If there are duplicates or other questions, the user will not be asked, but forced to copy;
-I: If the destination file (destination) already exists, it will ask if the action is true when it is overwritten!
-L: The link file for the hard link is created, not the file itself;
-P: Copy the past with the attributes of the archive, not the default properties;
-R: Recursive continuous Replication for directory replication behavior;
-S: Copying as a symbolic link file (symbolic Link), which is the "shortcut" file,
-u: If destination is older than source, update destination!
Finally note that if the source file has more than two, then the last destination file must be "directory" line!


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]# # Repeat two times, because the BASHRC already exists in/TMP, plus the-i parameter,
# will ask the user to determine 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 under
[[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?! ~
# 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!
[[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 12:46 wtmp_2
# Understand! The entire data feature is exactly the same sunglass! This is a feature of-a!

Example Three: Copy all contents of the/etc/directory to/tmp under
[[email protected] tmp]# cp/etc//tmp
Cp:omitting Directory '/etc ' <== if it is a directory and cannot be copied directly, add the-r parameter
[[email protected] tmp]# cp-r/ etc//tmp
# or to emphasize again! -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 Oh!

Example Four: Create 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_s Link
[[[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
# that Bashrc_slink is caused by the-s parameter , the setting is a "shortcut",
# so you will see at the far right of the file, it will show that this file is "link" to Where to go!
# As for that Bashrc_hlink fun! After establishing this file, BASHRC and Bashrc_hlink
# All the same parameters, but the second column of the link number changed to 2 ~ instead of the original 1 Oh!
# 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, 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, here first 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.

This article is from the "12690807" blog, please be sure to keep this source http://12700807.blog.51cto.com/12690807/1939446

Linux CP command parameters and usage details---linux copy file command CP

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.