The CP command is one of the most common commands used to copy files or directories in Linux. The CP command is powerful and has a lot of usage.
CP Command:
cp-copy Files and directories
Grammar:
CP [OPTION] ... [-T] SOURCE dests
CP [OPTION] ... SOURCE ... DIRECTORY
CP [OPTION] ...-t DIRECTORY SOURCE ...
Options:
-A,--archive same as-dr--preserve=all
Effect and same as specifying "-DR--preserve=all"
--attributes-only don ' t copy the file data, just the attributes
Copy only properties do not copy file data
--backup[=control] Make a backup of each existing destination file
Make a backup of each existing target file
-B like--backup but does don't accept an argument
Similar to--backup but does not accept parameters
-d Same as--no-dereference--preserve=links
Copy only link names without copying source files
-F,--force if an existing destination file cannot being opened, remove it and try again (this option is ignore D when the-n option is also used)
Force replication If the destination file cannot be opened, move it out and try again (this option is not required when the-n option is present)
-I,--interactive prompt before overwrite (Overrides a previous-n option)
Interactive, pre-overwrite ask (invalidates the previous-n option)
-H Follow command-line symbolic links in SOURCE
Copy the symbolic link and follow the command-line symbolic link in the source file, that is, copy the file to the symbolic link point.
-L,--link hard link files instead of copying
Create a hard-link file on the source file without copying it
-L,--dereference always follow symbolic links in SOURCE
Always follow the symbolic linked file
-N,--no-clobber do not overwrite an existing file (Overrides a previous-i option)
Do not overwrite existing files (invalidates the previous-i option)
-P,--no-dereference never follow symbolic links in SOURCE
Do not follow a symbolic link in the source file
-p Same as--preserve=mode,ownership,timestamps
Similar to--preserve=mode,ownership,timestamps, the permission to copy files together, belong to the owner, belong to the group
--preserve[=attr_list] "Property list"
Preserve the specified attributes (default:mode,ownership,timestamps), if possible additional attributes:context, links , Xattr, all
Keep the specified property (default: Permission, owner, timestamp), or add additional attributes (security context, link properties, extended properties of the file, etc.)
-c deprecated, same as--preserve=context
Similar to--preserve=context
--no-preserve=attr_list don ' t preserve the specified attributes
Does not preserve the specified property
--parents use full source file name under DIRECTORY
Use the full file name under the source directory
-R,-R,--recursive copy directories recursively
Recursively replicate directories and all internal content
--reflink[=when] control clone/cow copies. See below
Control below
-S,--symbolic-link make symbolic links instead of copying
Create soft links to source files without copying files
-S,--suffix=suffix override the usual backup suffix
After backing up the destination file with the-b parameter, the tail of the backup file is called the previous backup string, and the default backup string is the symbol "~"
-T,--target-directory=directory Copy all SOURCE arguments to directory
Copy all the parameters of the source file to the destination file
-T,--no-target-directory treat DEST as a normal file
Treat the target file as a normal file
-X,--one-file-system stay on the This file system
The file system in which the copied file or directory resides must be the same as the file system where the CP command executes, otherwise it will not be copied or processed in other partitions
-U,--update copy only if the SOURCE file is newer than the destination file or when the destination file is missing Copy only if the source file is not new or the destination file is missing
-V,--verbose explain what's being done
Show replication process
--help Display this Help and exit
Show online Help
--version output version information and exit
Display version Information
How to use:
CP SRC (source file) DEST (target file)
SRC is a file:
If the target does not exist: Create a new dest and populate the SRC content into dest
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m00/85/21/wkiol1eavskrmqnxaaatava-yrs627.png "title=" 1.PNG "alt=" Wkiol1eavskrmqnxaaatava-yrs627.png "/>
If the target exists:
If Dest is a file: Overwrite content in SRC to dest based on security, we recommend that you use the-i option for the CP command
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m00/85/21/wkiol1eavuatzh2paaatypdalxg284.png "title=" 2.PNG "alt=" Wkiol1eavuatzh2paaatypdalxg284.png "/>
If Dest is a directory: Create a new file with the same name as the original file under Dest and populate the contents of SRC with the new file
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m02/85/21/wkiol1eavw_jqar1aaamcfzetky478.png "title=" 3.PNG "alt=" wKioL1eavW_ Jqar1aaamcfzetky478.png "/>
CP SRC ... DEST
Src... : Multiple file dest must exist and be directory, other situations will be error;
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/85/21/wKiom1eavZHy8ueYAAAv_Y1Ky_U973.png "title=" 4.PNG " alt= "Wkiom1eavzhy8ueyaaav_y1ky_u973.png"/>650) this.width=650; src=/e/u261/themes/default/images/spacer.gif "Style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>
CP SRC DEST
SRC is the directory: Use this time option:-R
If Dest does not exist: creates the specified directory and copies all files in the SRC directory to dest;
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m02/85/21/wkiol1eavb_xt1nbaaaz3opq5-u116.png "title=" 5.PNG "alt=" wKioL1eavb_ Xt1nbaaaz3opq5-u116.png "/>
If dest exists: If Dest is a file: error, if dest is directory then copy directory and contents
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s2.51cto.com/wyfs02/m01/85/21/wkiom1eavewbvfltaaajzl5gdmg193.png "title=" 6.PNG "alt=" Wkiom1eavewbvfltaaajzl5gdmg193.png "/>
CP Copy Relationship Table
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/85/21/wKioL1eavknT8QRHAAFV7FWb4rs670.png "title=" Z.png " alt= "Wkiol1eavknt8qrhaafv7fwb4rs670.png"/>
Example: 1, using the alias command, the daily/etc/directory of all files, backup to/testdir/under the new directory, and the new directory format is BACKUPYYYY-MM-DD, the backup process is visible
[[email protected] ~]# alias backup= ' cp-rv/etc//testdir/backup ' date +%f '
[[Email protected] ~]# backup
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/85/21/wkiol1eavlvaelhfaaaustx0iu4773.png "title=" 7.PNG "alt=" Wkiol1eavlvaelhfaaaustx0iu4773.png "/>
2, first create the/testdir/rootdir directory, and then copy the/root all the next file into the directory, and ask to retain the original permissions
[email protected] ~]# CP--preserve=mode-r/root/testdir/rootdir
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/85/21/wkiom1eavm3yxfauaabinqk-00g760.png "title=" 8.PNG "alt=" Wkiom1eavm3yxfauaabinqk-00g760.png "/>
This article is from the "I ' m Groot" blog, so be sure to keep this source http://groot.blog.51cto.com/11448219/1831598
Linux Basic command Copy command CP