One Linux command per day (6): CP

Source: Internet
Author: User

1. Introduction to Commands

cp(copy file): Copy the source file to the destination file, or copy multiple source files to the destination directory.

2. Usage
     CP [options] ... [-T] Source file destination file or: CP [Options] ... Source file ... Directory or:cp [options] ...-t directory source files ...
3. Options
  -A,--archive       equals-dr--preserve=all, same as specifying the-DPR three options effect, to replicate the entire directory, including subdirectories in the directory, such as recursive replication, but also to maintain the file access mode, owner, Attributes such as timestamps are the same as the original file.       --backup[=CONTROL        creates backups  for each existing target file                -B-similar-backup but does not accept      parameters --copy-  Contents        in recursive processing is to copy special file contents                  -D equals--no-dereference--preserve=  links-F,--force            if the target file cannot be opened, remove it and tryagain (you do not need to select this when the-N option exists  )-I,--interactive        before overwriting (invalidates the previous-n option), The default CP command overwrites the target file is not prompted, many Linux distributions in the CP are set alias ' Cp-i ', in fact, the role is to give users a reminder. If you do not want to be prompted, then enter: \CP source target, or use the absolute path of the  CP command/BIN/CP-H                follows the command-line symbolic  link in the source file ----- Link            creates a hard link to the source file rather than copying the file
  -L,--dereference always follow the symbolic link-N,--no-clobber do not overwrite existing files (so that the previous-i option is disabled)-P,--no-Dereference does not follow the symbolic link in the source file-P equals--preserve=mode, ownership, time stamp--preserve[=The property list retains the specified property (default: Mode, ownership, timestamp) if additional properties may be maintained: environment, links, xattr, etc.-C Same as--preserve=Context--sno-preserve=Property list does not preserve the specified file attributes--Parents all directories in the destination directory where the source file path was created before copying-R,-R,--recursive recursively replicate all content within a directory and its subdirectories--reflink[=when] control cloning/CoW copy.      Please check the Neru below. --remove-destination before attempting to open the destination file, delete the existing destination file (relative to--Force option)--sparse=When to control how sparse files are created--strip-trailing-slashes Delete all source files in Parameters/Slash at the end of the directory-S,--symbolic-link creates symbolic links only and does not copy files-S,--suffix=Suffix Specifies the suffix of the backup file by itself-T, the--target-directory= directory assigns all parameters to the source file/directory copied to target directory-T,--no-target-Directory regards the target directory as a normal file-U,--update uses this parameter only to copy files when the source file is changed more than when the target file is updated or if the name of the target file does not exist .-V,--verbose details the actions performed by the command. -X,--one-file-system the file or directory in which the files or directories are stored must be the same as the file system in which the CP instruction is executed, otherwise it will not be copied or processed in other partitions.
  -Z,--context=context        Set security context of copy to context
4. Instance 1: Copy file a.txt to file B.txt
CP A.txt  b.txt
Example 2: Copy file a.txt to file B.txt, show details
CP -v a.txt  b.txt ' a.txt', '  b.txt'
Example 3: Copying a file only when the source file has been modified for the destination file is new

CP -UV  a.txt  c.txt

Example 4: The file a.txt is copied into a file by interactive mode D.txt
CP -IV a.txt  d.txtcp: Overwrite ' d.txt'? y' a.txt'  - > ' d.txt'
Example 5: Copy the file a.txt to D.txt because the destination file already exists, so specify the mode to use forced replication

CP -FV a.txt  d.txt ' a.txt', '  d.txt'

Example 6: Recursively copy directory Dir1 to directory Dir2 (Dir2 does not exist at this time)
 [[email protected] directory]# CP -RV Dir1 dir2 ' dir1   " " Dir2   "  ' dir1 /c.txt   "dir2/c.txt  "  " Dir1 /a.txt " " dir2/ A.txt   "  ' dir1 /b.txt"   "dir2/b.txt  "   ' dir1 /d.txt " -"Dir2/d.txt   " 
Example 7: Recursively copy directory Dir1 to directory Dir2 (Dir2 already exists)
CP -RV  dir1  dir2 ' dir1/c.txt ', ' dir2/dir1/c.txt' Dir1/a.txt ', ' dir2/dir1/a.txt'dir1/b.txt'  - > ' dir2/dir1/b.txt'dir1/d.txt '  dir2/dir1/d.txt' 
Example 8: Preserve file properties while copying

[email protected] directory]# lltotal0-rwxrwxrwx1Root root0Apr -  -: WuA.txt[[email protected] directory]#CPa.txt/tmp/A1.txt[[email protected] directory]#CP-P a.txt/tmp/A2.txt[[email protected] directory]# ll/Tmptotal A-rwxr-xr-x1Root root0Apr -  -: AboutA1.txt-rwxrwxrwx1Root root0Apr -  -: WuA2.txt

Example 9: Backup files are generated when copying
[Email protected] directory]#CP-BV a.txt/tmp/' A.txt''/tmp/a.txt'[email protected] directory]#CP-BV a.txt/tmp/' A.txt''/tmp/a.txt'(Backup: '/tmp/a.txt~')[email protected] directory]# LL/Tmptotal0-rwxr-xr-x1Root root0Apr -  -: Genevaa.txt-rwxr-xr-x1Root root0Apr -  -: Genevaa.txt~
Example 10: Create Hard links to files (with the same inode) instead of copying them
[Email protected] dir1]#CP-l a.txt b.txt[[email protected] dir1]#CPa.txt c.txt[[email protected] dir1]#ls-Total Li dosage04718769-rw-r--r--2Root root0April -  -: -a.txt4718769-rw-r--r--2Root root0April -  -: -B.txt4718772-rw-r--r--1Root root0April -  -: -C.txt
Example 11: Copy the A.txt to create a Nexus file A_link.txt
CP -SV a.txt  a_link.txt ' a.txt'  a_link.txt'0     1 5: a_link.txt-a.txt  1 0  - : A.txt
12: Do not copy original file with symbolic link
[[email protected] dir1]# ll total dosage0lrwxrwxrwx1Root root5April -  -: -A_link.txta.txt-rw-r--r--1Root root0April -  -: -A.txt[[email protected] dir1]#CP-P a_link.txt C.txt[[email protected] dir1]# ll total dosage0lrwxrwxrwx1Root root5April -  -: -A_link.txta.txt-rw-r--r--1Root root0April -  -: -a.txtlrwxrwxrwx1Root root5April -  -: toC.txt-A.txt
Example 13: Specifying the backup file footer
[Email protected] dir1]#CP-v-s _bak a.txt/tmp/"a.txt"-"/tmp/a.txt"[email protected] dir1]#CP-v-s _bak a.txt/tmp/CP: Whether to overwrite"/tmp/a.txt"? Y"a.txt"-"/tmp/a.txt"Backup"/tmp/a.txt_bak")

One Linux command per day (6): CP

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.