Linux in the system cp (copy) the command is used to copy files or directories.
Man CP can get CP a detailed description of
Command format:
CP [OPTION] ... [-T] SOURCE DEST//CP [ options ] ... [-T] Source Purpose
CP [OPTION] ... SOURCE ... DIRECTORY//CP [ options ] ... Source ... Catalogue
CP [option] ...-t directory source ...//CP [ option ]...-t directory sources ...
Function:
Copy source to DEST, or multiple source (s) to DIRECTORY.
Copy the source file to the destination file, or copy multiple source files to the destination directory.
Parameters:
-A This option is typically used when copying a directory. It retains links, file attributes, and recursively copies the directory, which is equal to the combination of the DpR option.
*-D Copy the symbolic link file itself, not the source file it points to
* ( Use caution )-F mandatory meaning, delete the existing target file without prompting.
*-I. Interactive replication, that is, overwriting before alerting the user to confirm
-P At this point the CP will also copy its modification time and access rights to the new file, in addition to copying the contents of the source file. (Backup common)
*-R recursive continuous replication for directory replication behavior : If the given source file is a directory file, the CP will recursively replicate all subdirectories and files in that directory. The destination file must be a directory name at this time.
-L is not a copy, just a link file, not a copy of the file itself .
-S copy into a symbolic link file (symbolic link), which is the "shortcut" file
-U is only updated when the target is older than the source file, often used for "backup" work
Command columns:
1. -I parameter example, which prompts you before overwriting
[[email protected] ~]# cp ~/.BASHRC/TMP/BASHRC Copy and rename operation [[email protected] ~]# cp-i ~/.BASHRC/TMP/BASHRC due to After copying, adding the-I parameter, before overwriting will do hint cp:overwrite '/TMP/BASHRC '? y = Confirm by y
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/7E/8F/wKioL1cEst-jjXT5AADHjalobBk975.jpg "title=" 1.jpg " alt= "Wkiol1cest-jjxt5aadhjalobbk975.jpg"/>
2.-a parameter examples, copy the permissions, and so on.
[[email protected] ~]# cd/tmp into/tmp directory [[email protected] tmp]# cp/var/log/wtmp. To copy to the current directory, finally add "." [Email protected] tmp]# ls-l/var/log/wtmp wtmp-rw-rw-r--. 1 root utmp 24960 Apr 6 14:06/var/log/wtmp-rw-r--r--1 root root 24960 Apr 6 14:39 wtmp
650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M02/7E/93/wKiom1cEsmzjQrlQAAEGX6hwS_E414.jpg "title=" 2.jpg " alt= "Wkiom1cesmzjqrlqaaegx6hws_e414.jpg"/>
Some properties/permissions change without any parameters
The file creation time is also different.
If you want to copy all the attributes of a file together, you can add a-a, as shown here
[Email protected] tmp]# cp-a/var/log/wtmp wtmp3[[email protected] tmp]# ls-l/var/log/wtmp wtmp3-rw-rw-r--. 1 root utmp 24960 APR 6 14:06/var/log/wtmp-rw-rw-r--. 1 root utmp 24960 APR 6 14:06 Wtmp3
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/7E/93/wKiom1cEsrjBE2bkAAD6zRKrpz4981.jpg "title=" 3.jpg " alt= "Wkiom1cesrjbe2bkaad6zrkrpz4981.jpg"/>
The entire data feature is exactly the same, which is the feature of-a
Because of this feature, when we are doing backup, some special privilege files that need special attention, such as password file (/etc/shadow) and some configuration file, can not be copied directly with CP, but must be added with a-a or-p and so on to fully copy the file permissions of the option! In addition, if you want to copy the file to other users, you must also notice the permissions of the file (including read, write, run, and file owner, etc.), otherwise, other people will not be able to make changes to the files you give the action Oh! Pay attention!
3.-R Example , Copy all directories under directory including subdirectories to another directory
[[email protected] tmp]# cp/etc//tmp cp:omitting directory '/etc/' if it is a directory, it cannot be copied directly [email protected] tmp]# cp-r/etc//tmp to join-R to copy directory [[email protected] tmp]#
650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/7E/8F/wKioL1cEtcfAHt_rAACXOU8cyTo086.jpg "title=" 4.jpg " alt= "Wkiol1cetcfaht_raacxou8cyto086.jpg"/>
4.-s,-l examples, creating soft links and hard links
[[email protected] tmp]# ls -l bashrc -rw-r--r-- 1 Root root 206 apr 6 14:24 bashrc[[email protected] tmp]# cp -s bashrc bashrc_slink Create a soft link, which is a shortcut [[ email protected] tmp]# cp -l bashrc bashrc_hlink Create a hard link [[email protected] tmp]# ls -l bashrc*-rw-r--r-- 2 root root 206 Apr 6 14:24 bashrc source file I-node, is 2-rw-r--r-- 2 root root 206 Apr 6 14:24 bashrc_hlink Hard link I-node, is 2lrwxrwxrwx 1 root root 6 apr 6 15:07 bashrc_slink -> bashrc Soft Link I-node, or 1
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/7E/8F/wKioL1cEt43SIIadAAICLksP1qQ385.jpg "title=" 5.jpg " Width= "730" height= "241" border= "0" hspace= "0" vspace= "0" alt= "wkiol1cet43siiadaaiclksp1qq385.jpg" style= "width : 730px;height:241px; "/>
5. -U example , if ~./BASHRC is newer than /tmp/bashrc, copy it .
[[email protected] tmp]# cp-u ~/.BASHRC/TMP/BASHRC often used for backup work
6-d example, copy Bashrc_slink to bashrc_slink_1 and bashrc_slink_2
[[EMAIL PROTECTED] TMP]# CP BASHRC_SLINK BASHRC _slink_1[[email protected] tmp]# cp -d bashrc_slink bashrc_slink_2[[email Protected] tmp]# ls -l bashrc bashrc_slink*-rw-r--r-- 2 root root 206 apr 6 14:24 bashrclrwxrwxrwx 1 root root 6 Apr 6 15:22 bashrc_slink -> bashrc-rw-r--r-- 1 root root 206 apr 6 15:22 bashrc_slink_1 Same as source file lrwxrwxrwx 1 root root 6 apr 6 15:22 bashrc_slink_2 -> bashrc is the connection file
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7E/90/wKioL1cEusLyfahCAAHu2MjKaCI374.jpg "title=" 6.jpg " Width= "730" height= "223" border= "0" hspace= "0" vspace= "0" alt= "wkiol1ceuslyfahcaahu2mjkaci374.jpg" style= "width : 730px;height:223px; "/>
This example is also very interesting oh! The original copy is the link file, but the link file to copy the actual files.
that is, if you do not add any options,CP copies the original file, not the properties of the link file!
To copy the properties of a link file, you have to use the-d option! As shown in the Bashrc_slink_2.
7. With zdw users, full replication
[Email protected] ~]$ cp-a/var/log/wtmp/tmp/zdw_wtmp[[email protected] ~]$ ls-l/var/log/wtmp/tmp/zdw_wtmp-rw-rw- r--1 zdw zdw 24960 APR 6 14:06/tmp/zdw_wtmp-rw-rw-r--. 1 root utmp 24960 APR 6 14:06/var/log/wtmp
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/7E/93/wKiom1cEvVHwZc_OAAEstsmIhRw262.jpg "title=" 7.jpg " Width= "730" height= "147" border= "0" hspace= "0" vspace= "0" alt= "wkiom1cevvhwzc_oaaestsmihrw262.jpg" style= "width : 730px;height:147px; "/>
Because the identity of ZDW can not arbitrarily modify the file owner and group, so although the ability to copy wtmp related permissions and Time properties, but with the owner, group-related, the original ZDW identity can not be done, even with the-a option, it is not possible to achieve full copy permissions!
8. Copy multiple data to the specified directory at once
[[email protected] tmp]# CP ~/.BASHRC ~/.BASH_HISTORY/TMP can fill in multiple data, copy to a directory
This article is from the "Zhao Dongwei blog" blog, make sure to keep this source http://zhaodongwei.blog.51cto.com/4233742/1871242
Linux command (+): CP command