One Linux command per day-CP

Source: Internet
Author: User

CP command is used for file and directory replication, is one of the common Linux commands, in general, the Shell will set an alias cp= ' Cp-i ', when copying files at the command line, if the target file already exists, will ask whether to overwrite, Regardless of whether you use the-i parameter. However, if you execute the CP in a shell script, you do not ask for overrides if you do not have the-i parameter. This means that the command line and shell scripts perform somewhat differently.  

1. Command format:     

CP [OPTION] ... [-T] SOURCE DEST

CP [OPTION] ... SOURCE ... DIRECTORY

CP [OPTION] ...-t DIRECTORY SOURCE ...


2. Command parameters:

CP [-AIFPRU] [source file] [target file]

-A: equivalent to-PDR

-D: If the source file is a linked file (like file), the linked file property is copied instead of the file itself

-F: Mandatory, if the target file already exists and cannot be opened, after the removal is attempted

-I: If the target file already exists, it will be asked before overwriting;

-P: Copied along with the attributes of the archive, rather than using default properties

-R: Recursive for copying directories

-U: If the target file exists. The target file is copied before the source file;

3. Command instance:

1. Copy a single file to the destination directory, and the file does not exist in the destination directory.

[[email protected] ~]# CP a.txt Backup/[[email protected] ~]# ll a.txt-rw-r--r--. 1 root root 4 June 14:56 A.txt[[email protected] ~]# ll backup/total 4-rw-r--r--. 1 root root 4 Jul 4 22:07 a.txt[[email protected] ~]#

Note: You will find that the timestamp of two files is different, if you want the target file and the source file properties, you can use the-a option

2. Copy a single file to the destination directory, the file already exists in the destination directory, and the copied file is the same as the source file properties.

[email protected] ~]# ll a.txt-rw-r--r--. 1 root root 4 June 14:56 A.txt[[email protected] ~]# cp-a a.txt backup/cp:overwrite ' backup/a.txt '? Y[[email protected] ~]# ll backup/total 4-rw-r--r--. 1 root root 4 June 14:56 A.txt[[email protected] ~]#

Note: When copying files, CP will prompt for overwriting files if the files in the target directory are already present, because the CP under the command line is aliased, using alias | grep "CP" view CP alias, alias Cp= ' Cp-i ', the-i parameter is if the target file already exists, will be asked when overwriting, if you do not want to prompt for direct coverage, you can cp-n or/BIN/CP.

3. Copy the directory:

When the destination directory does not exist

[[Email protected] ~]# lsanaconda-ks.cfg  a.txt  backup  b.txt   c.txt  data  d.txt  ett.txt  e.txt  install.log   install.log.syslog  mkdirtest  oldboy.txt  readme.txt   Test[[email protected] ~]# cp -a mkdirtest mkdirtest2[[email protected]  ~]# ll mkdirtest2total 24drwxr-xr-x. 2 root root 4096 Jun  29 02:38 bindrwxr-xr-x. 4 root root 4096 jun 29 02:38  Configdrwxr-xr-x. 4 root root 4096 jun 29 02:38 infodrwxr-xr-x. 2  root root 4096 jun 29 02:38 libdrwxr-xr-x. 2 root root  4096 jun 29 02:38 lib32drwxr-xr-x. 2 root root 4096 jun  29 02:38 sbin[[email protected] ~]# ll mkdirtesttotal 24drwxr-xr-x. 2 root root 4096  Jun 29 02:38 bindrwxr-xr-x. 4 root root 4096 Jun 29  02:38 configdrwxr-xr-x. 4 root root 4096 jun 29 02:38  Infodrwxr-xr-x. 2 root root 4096 jun 29 02:38 libdrwxr-xr-x. 2  root root 4096 jun 29 02:38 lib32drwxr-xr-x. 2 root root  4096 jun 29 02:38 sbin

When the destination directory already exists

[[email protected] ~]# mkdir Mkdirtest3[[email protected] ~]# ll mkdirtest3total 0[[email protected] ~]# cp-a mkdirtest m Kdirtest3[[email protected] ~]# ll mkdirtest3total 4drwxr-xr-x. 8 root root 4096 June 02:38 Mkdirtest[[email protected] ~]#

Description: When copying a directory, when the destination directory does not exist, the target directory is created first, and then the contents of the source directory are copied to the destination directory, and the source directory is copied directly to the existing target directory when the destination directory exists.

One Linux command per day-CP

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.