Introduction to the Linux CP (copy) command

Source: Internet
Author: User

\CP -RF source1 source2 source3 .... Directory

CP (copy files or directories)
[[email protected] ~]# CP [-ADFILPRSU] source file (source) destination (destination) [[email protected] ~]# CP [options] Source1 source2 sou Rce3 .... Directory

Parameters:

-A: equivalent to the meaning of-pdr;
-D: If the source file is the attribute of the link file, the link file attribute is copied instead of 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, it will ask if the action is true when it is overwritten!
-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

# Did you notice?! In the case of no parameters, the file's owner will change, and even the permissions have changed ~

# 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!

Example three: Copy all the contents of the/etc/directory to/tmp

[[email protected] tmp]# cp/etc//tmpcp:omitting directory ' etc '    <== if it is a directory, cannot be copied directly, add-r parameters [[email protected] t mp]# 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]# cp-s BASHRC bashrc_slink[[email protected] tmp]# cp-l BASHRC bashrc_hlink

# 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, 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 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!

Attention:

When the copied object is " directory (that is, folder)", you must remember to add the parameter- R

Therefore, the common Copy command format is as follows:

CP -RF source1 source2 source3 .... Directory

Description: I usually use the \cp-rf command First, and then use chown and chmod to modify the properties.

Recommended Reading

Methods to resolve invalid commands for CP-F replication forced overwrite in Linux CentOS

Introduction to the Linux CP (copy) command

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.