Copy all statements in the cp command in CentOS

Source: Internet
Author: User

Today, when writing a script, we found a strange problem: when we use cp to copy all the files in the current directory to the target directory, the source and target directories are of different sizes. I didn't pay attention to this problem until I checked some materials to find out that the format I used was incorrect ,.
I. Preparation
Cp is a copy, and the simplest way to use it is:

Cp oldfile newfile
However, in this case, only files can be copied and directories cannot be copied. Therefore, the following commands are usually used:
Cp-r old/new/
The old directory will be copied to the new directory. Note: instead of copying the files in the old directory to the new directory, the old directory is directly copied to the new directory. The result is:
Reference [root @ dc5 test] # ll new/
Total 4
Drwxr-xr-x 2 root 4096 Dec 15 11: 55
If you want to keep all the permissions of the source file, you can:
Cp-rp old/new/
-P parameter, which can maintain permissions, host, time stack, and link. It is simpler to use:
Cp-a old/new/
-A parameter is equal to-dpR.
Ii. Question 1
Okay. Let's take a look at this issue. Environment:
◎ Two directories: old and new. The old directory contains three contents: the test1 file, the test2 directory, and The. test3 directory. This is an implicit file.
Reference [root @ dc5 test] # ll-laR
.:
Total 20
Drwxr-xr-x 4 root 4096 Dec 15 :55.
Drwxrwxrwt 7 root 4096 Dec 15 11: 59 ..
Drwxr-xr-x 2 root 4096 Dec 15 12:14 new
Drwxr-xr-x 3 root 4096 Dec 15 :14 old

./New:
Total 8
Drwxr-xr-x 2 root 4096 Dec 15 12:14.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..

./Old:
Total 12
Drwxr-xr-x 3 root 4096 Dec 15 12:14.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..
-Rw-r -- 1 root 0 Dec 15 :07. test3
-Rw-r -- 1 root 0 Dec 15 12:05 test1
Drwxr-xr-x 2 root 4096 Dec 15 12:14 test2

./Old/test2:
Total 8
Drwxr-xr-x 2 root 4096 Dec 15 12:14.
Drwxr-xr-x 3 root 4096 Dec 15 12:14 ..
◎ Operation 1:
Reference [root @ dc5 test] # cp-a old/* new/
[Root @ dc5 test] # ll-laR new/
New /:
Total 12
Drwxr-xr-x 3 root 4096 Dec 15 12:15.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..
-Rw-r -- 1 root 0 Dec 15 12:05 test1
Drwxr-xr-x 2 root 4096 Dec 15 12:14 test2

New/test2:
Total 8
Drwxr-xr-x 2 root 4096 Dec 15 12:14.
Drwxr-xr-x 3 root 4096 Dec 15 12:15 ..
The problem arises: The Hidden. test3 files are not all copied to the new directory.
The reason is: * The parameter usage is incorrect. This is usually because you are familiar with the Dos format (including my own), but in the bash environment, cp cannot match an implicit file starting.
◎ Operation 2
The correct statement should be as follows:
Reference [root @ dc5 test] # cp-a old/. new/
[Root @ dc5 test] # ll-laR new/
New /:
Total 12
Drwxr-xr-x 3 root 4096 Dec 15 12:14.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..
-Rw-r -- 1 root 0 Dec 15 :07. test3
-Rw-r -- 1 root 0 Dec 15 12:05 test1
Drwxr-xr-x 2 root 4096 Dec 15 12:14 test2

New/test2:
Total 8
Drwxr-xr-x 2 root 4096 Dec 15 12:14.
Drwxr-xr-x 3 root 4096 Dec 15 12:14 ..
Use a. number instead of a * number.
There is also a complicated way of writing:
Reference [root @ dc5 test] # cp-a old/* old/. [^.] * new/
[Root @ dc5 test] # ll-laR new/
New /:
Total 12
Drwxr-xr-x 3 root 4096 Dec 15 12:25.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..
-Rw-r -- 1 root 0 Dec 15 :07. test3
-Rw-r -- 1 root 0 Dec 15 12:05 test1
Drwxr-xr-x 2 root 4096 Dec 15 12:14 test2

New/test2:
Total 8
Drwxr-xr-x 2 root 4096 Dec 15 12:14.
Drwxr-xr-x 3 root 4096 Dec 15 12:25 ..
Note the writing method. Do not write it. (See the following for the reason)
Iii. Question 2
Do not write it as. *. What does. * represent?
Reference [root @ dc5 test] # echo .*
...
. * Indicates the current directory and the previous directory.
Therefore, using. * causes a larger problem:
Reference [root @ dc5 test] # cp-a old/. * new/
Cp: cannot copy a directory, 'old/.. ', into itself, 'new /'
Cp: cannot copy a directory, 'old/.. ', into itself, 'new /'
Cp: will not create hard link 'new/old' to directory 'new /.'
Cp: overwrite 'new/. test3 '? Y
[Root @ dc5 test] # ll-laR new/
New /:
Total 16
Drwxr-xr-x 4 root 4096 Dec 15 :55.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..
-Rw-r -- 1 root 0 Dec 15 :07. test3
Drwxr-xr-x 2 root 4096 Dec 15 12:14 new
-Rw-r -- 1 root 0 Dec 15 12:05 test1
Drwxr-xr-x 2 root 4096 Dec 15 12:14 test2

New/new:
Total 8
Drwxr-xr-x 2 root 4096 Dec 15 12:14.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..
-Rw-r -- 1 root 0 Dec 15 :07. test3
-Rw-r -- 1 root 0 Dec 15 12:05 test1

New/test2:
Total 8
Drwxr-xr-x 2 root 4096 Dec 15 12:14.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..
That is to say, using. * is like this:
Reference [root @ dc5 test] # cp-a old/. old/... old/. test3 new/
[Root @ dc5 test] # echo old /.*
Old/. old/... old/. test3
Iv. Expansion

In fact, not only do cp commands have such problems, but all commands involving files with special characters must be considered, such as rm:

Reference [root @ dc5 new] # ll-
Total 12
Drwxr-xr-x 3 root 4096 Dec 15 12:14.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..
-Rw-r -- 1 root 0 Dec 15 :07. test3
-Rw-r -- 1 root 0 Dec 15 12:05 test1
Drwxr-xr-x 2 root 4096 Dec 15 12:14 test2
[Root @ dc5 new] # rm-rf *
[Root @ dc5 new] # ll-
Total 8
Drwxr-xr-x 2 root 4096 Dec 15 12:40.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..
-Rw-r -- 1 root 0 Dec 15 :07. test3
The correct statement should be:
Reference [root @ dc5 new] # rm-rf .**
Rm: cannot remove '.' or '..'
Rm: cannot remove '.' or '..'
[Root @ dc5 new] # ll-
Total 8
Drwxr-xr-x 2 root 4096 Dec 15 12:42.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..
Of course, this is the same:
Reference [root @ dc5 new] # rm-rf *. [^.] *
[Root @ dc5 new] # ll-
Total 8
Drwxr-xr-x 2 root 4096 Dec 15 :44.
Drwxr-xr-x 4 root 4096 Dec 15 :55 ..
※In many cases, the expected and actual results are completely different. Note especially when writing scripts in bash.


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.