One linux command every day (8): cp command

Source: Internet
Author: User
One linux command every day (8): cp command link: One linux command every day (1): ls command http://www.2cto.com/ OS /210210/163049.html#linuxlinuxcommand every day (2): cd command quit (..
One linux command every day (8): cp command link: One linux command every day (1): ls command http://www.2cto.com/os/201210/163049.html ; One linux command every day (2): cd command http://www.2cto.com/os/201210/163050.html ; One linux command every day (3): pwd command http://www.2cto.com/os/201210/163462.html ; One linux command every day (4): mkdir command http://www.2cto.com/os/201210/163463.html ; One linux command every day (5): rm command http://www.2cto.com/os/201210/163662.html ; One linux command (6) every day: rmdir command http://www.2cto.com/os/201210/164017.html ; One linux command (7) every day: mv command http://www.2cto.com/os/201210/164247.html The cp command is used to copy files or directories. it is one of the most common commands in Linux. In general, shell sets an alias. when copying a file under the command line, if the target file already exists, it will ask whether to overwrite the file, whether or not you use the-I parameter. However, if the cp is executed in a shell script, if the-I parameter is not displayed, the system does not ask whether to overwrite it. This indicates that the command line and shell script are executed differently. 1. command format: www.2cto.com usage: cp [option]... [-T] source target or: cp [option]... source... directory or: cp [option]... -t directory source... 2. command: copy the source file to the target file, or copy multiple source files to the target directory. Www.2cto.com 3. command parameter:-, -- archive is equal to-dR -- preserve = all -- backup [= CONTROL creates a backup for each existing target file-B is similar to -- backup but does not accept the parameter -- copy-contents in recursive processing is copy the content of a special file-d equals to -- no-dereference -- preserve = links-f, -- force: if the target file cannot be opened, remove it and try again (this option is not required when the-n option exists)-I, -- interactive overwrite the previous query (invalidate the previous-n option)-H follows the command line symbolic link-l in the source file, -- link file without copying-L, -- dereference always follows the symbolic link-n, -- no-clobber does not overwrite existing files (make the previous-I option invalid)-P, -- no-dereference does not follow the symbolic link in the source file -P equals to -- preserve = mode, ownership, timestamp -- preserve [= attribute list to keep the specified attribute (default: mode, ownership, timestamp). if it is possible to keep the additional attribute: environment, link, xattr, etc.-R,-r, -- recursive copy directory and all the projects in the directory www.2cto.com 4. command instance: instance 1: copy a single file to the target directory. the file does not exist in the target file. Command: cp log. log test5 output: [root @ localhost test] # cp log. log test5 [root @ localhost test] # ll-rw-r -- 1 root 0 10-28 log. logdrwxr-xr-x 6 root 4096 10-27 0:58 scfdrwxrwxrwx 2 root 4096 10-28 test3drwxr-xr- X 2 root 4096 10-28 test5 [root @ localhost test] # cd test5 [root @ localhost test5] # ll-rw-r -- 1 root 0 10- 28 log5-1.log-rw-r -- r -- 1 root 0 10-28 log5-2.log-rw-r -- r -- 1 root 0 10-28 log5-3.log-rw-r -- r -- 1 root 0 10-28 log. log description: When the-a parameter is not included, the time of the two files is different. When the-a parameter is included, the time of the two files is the same. Www.2cto.com instance 2: If the target file exists, you will be asked whether to overwrite the command: cp log. log test5 output: [root @ localhost test] # cp log. log test5cp: whether to overwrite "test5/log. log "? N [root @ localhost test] # cp-a log. log test5cp: overwrite "test5/log. log "? Y [root @ localhost test] # cd test5/[root @ localhost test5] # ll-rw-r -- 1 root 0 10-28 log5-1.log-rw-r -- r -- 1 root root 0 10-28 log5-2.log-rw-r -- r -- 1 root 0 10-28 log5-3.log-rw-r -- r -- 1 root 0 10-28 log. log description: if the target file exists, you will be asked whether to overwrite it. This is because cp is the alias of cp-I. When the target file exists, even if the-f flag is added, the system will also ask whether to overwrite the file. Www.2cto.com instance 3: copy the entire directory command: Output: when the target directory exists: [root @ localhost test] # cp-a test3 test5 [root @ localhost test] # ll-rw-r -- 1 root 0 10-28 log. logdrwxr-xr-x 6 root 4096 10-27 0:58 scfdrwxrwxrwx 2 root 4096 10-28 test3drwxr-xr-x 3 root 4096 10-28 test5 [root @ localhost test] # cd test5/[root @ localhost test5] # ll-rw-r -- 1 root 0 10-28 log5-1.log-rw-r -- r -- 1 ro Ot root 0 10-28 log5-2.log-rw-r -- r -- 1 root 0 10-28 log5-3.log-rw-r -- r -- 1 root 0 10-28 log. logdrwxrwxrwx 2 root 4096 10-28 test3 target directory does not exist: [root @ localhost test] # cp-a test3 test4 [root @ localhost test] # ll-rw-r -- 1 root 0 10-28 log. logdrwxr-xr-x 6 root 4096 10-27 scfdrwxrwxrwx 2 root 4096 10-28 test3drwxrwxrwx 2 root 40 96 10-28 test4drwxr-xr-x 3 root 4096 10-28 test5 [root @ localhost test] # Note: The results of the existence of the target directory is not the same. If the target directory exists, the entire source directory is copied to the target directory. Www.2cto.com instance 4: The Copied log. log to create a link file log_link.log command: cp-s log. log log_link.log output: [root @ localhost test] # cp-s log. log log_link.log [root @ localhost test] # lllrwxrwxrwx 1 root 7 10-28 log_link.log-> log. log-rw-r -- 1 root 0 10-28 log. logdrwxr-xr-x 6 root 4096 10-27 0:58 scfdrwxrwxrwx 2 root 4096 10-28 test3drwxrwxrwx 2 root 4096 10-28 test4drwxr-xr -X 3 root 4096 10-28 test5 note: The log_link.log is caused by the-s parameter and creates a "shortcut 』, so you will see at the far right of the file, it will show where the file is connected!
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.