Linux Commands learning Note (8): CP command

Source: Internet
Author: User

The CP command is used to copy files or directories and is one of the most commonly used commands in a Linux system. In general, the shell sets an alias, and when the file is copied at the command line, if the target file already exists, it asks whether to overwrite it, whether or not you use the-I parameter. However, if you execute CP in a shell script, there is no-The i parameter does not ask whether to overwrite. This means that the command line and shell scripts perform somewhat differently. 1. Command format: Usage:CP[Options] ... [-T] Source purpose or:CP[Options] ... Source... Directory or:CP[Options] ...-T directory source ...2. Command function: Copy the source file to the destination file, or copy multiple source files to the target directory. 3. Command parameters:-A,--archive equals-dr--preserve= All--backup[=CONTROL creates a backup for each existing target file-B Similar-Backup but do not accept parameters--copy-contents in recursive processing is to copy special file contents-D equals--no-dereference--preserve=links-F,--force if the destination file cannot be opened, remove it and try again (when-N If the option exists, you do not need to select this item)-I,--interactive ask before overwriting (make the front-N option failed)-H Follow the command-line symbolic link in the source file-L,--link file without copying-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.-R,-R,--recursive copy all items in directory and directory4. Command instance: Instance one: Copy a single file to the destination directory, the file does not exist in the destination file command:CPlog.log test5 output: [[email protected] test]#CPlog.log Test5[[email protected] test]# ll-rw-r--r--1Root root0 Ten- -  -: -LOG.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096 Ten- -  -: -TEST3DRWXR-xr-x2Root root4096 Ten- -  -: -Test5[[email protected] test]# CD Test5[[email protected] test5]# ll-rw-r--r--1Root root0 Ten- -  -: $log5-1. Log-rw-r--r--1Root root0 Ten- -  -: $log5-2. Log-rw-r--r--1Root root0 Ten- -  -: $log5-3. Log-rw-r--r--1Root root0 Ten- -  -: -log.log Description: In the absence of-a parameter, the time of two files is different. In the band-a parameter, the time of two files is consistent. Instance two: When the target file exists, it asks whether to overwrite the command:CPlog.log test5 output: [[email protected] test]#CPLog.log test5CP: Do you want to overwrite "Test5/log.log"?N[[email protected] test]#CP-a log.log test5CP: Do you want to overwrite "Test5/log.log"?Y[[email protected] test]# CD TEST5/[email protected] test5]# ll-rw-r--r--1Root root0 Ten- -  -: $log5-1. Log-rw-r--r--1Root root0 Ten- -  -: $log5-2. Log-rw-r--r--1Root root0 Ten- -  -: $log5-3. Log-rw-r--r--1Root root0 Ten- -  -: -log.log Description: When the destination file exists, it asks whether to overwrite it. This is because CP is the CPThe alias of-I.. When the target file is present, even if it is added-The F flag will also ask whether to overwrite it. Example three: Copy the entire Directory command: output: When the target directory exists: [[email protected] test]#CP-a test3 test5 [[email protected] test]# ll-rw-r--r--1Root root0 Ten- -  -: -LOG.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096 Ten- -  -: -TEST3DRWXR-xr-x3Root root4096 Ten- -  the: OneTest5[[email protected] test]# CD TEST5/[email protected] test5]# ll-rw-r--r--1Root root0 Ten- -  -: $log5-1. Log-rw-r--r--1Root root0 Ten- -  -: $log5-2. Log-rw-r--r--1Root root0 Ten- -  -: $log5-3. Log-rw-r--r--1Root root0 Ten- -  -: -log.logdrwxrwxrwx2Root root4096 Ten- -  -: -test3 target directory does not exist: [[email protected] test]#CP-a test3 test4[[email protected] test]# ll-rw-r--r--1Root root0 Ten- -  -: -LOG.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096 Ten- -  -: -test3drwxrwxrwx2Root root4096 Ten- -  -: -TEST4DRWXR-xr-x3Root root4096 Ten- -  the: OneTest5[[email protected] test] #说明: Note that the destination directory exists or not, and the result is different. When the destination directory is present, the entire source directory is copied to the target directory. Example four: copy of Log.log Create a link file log_link.log command:CP-s Log.log log_link.log output: [[email protected] test]#CP-s Log.log log_link.log[[email protected] test]# lllrwxrwxrwx1Root root7 Ten- -  the: -Log_link.logLog.log-rw-r--r--1Root root0 Ten- -  -: -LOG.LOGDRWXR-xr-x6Root root4096 Ten- -  on: -scfdrwxrwxrwx2Root root4096 Ten- -  -: -test3drwxrwxrwx2Root root4096 Ten- -  -: -TEST4DRWXR-xr-x3Root root4096 Ten- -  the: Onetest5 Description: The Log_link.log was made by-The s parameter is created by a "shortcut", so you will see at the far right of the file where the file is "linked" to! 

Linux Commands learning Note (8): CP command

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.