Copy all the instructions in the Linux command

  • 2020-05-15 03:27:11
  • OfStack

Today, when I was writing a script, I found a strange problem: when I used cp to copy all the files in the current directory to the target directory, the source and target directories were different sizes. The original 1 straight did not pay attention to have such a problem, later looked up some information, only to know that the previous 1 straight used the format is wrong,

1. Prepare

cp is copy, and the easiest way to use it is:


cp oldfile newfile

But you can only copy files, not directories, so you usually use:


cp -r old/ new/

This will copy the entire old directory into the new directory. Note that instead of copying files from the old directory into the new directory, you copy old directly under new. The result is:


[root@dc5 test]# ll new/
total 4
drwxr-xr-x 2 root root 4096 Dec 15 11:55 old

If you want to keep all permissions on the source file, you can do this:


cp -rp old/ new/

-p parameter, which can maintain permissions, host, time stack, and possibly include link, etc.; Or, even simpler, use:


cp -a old/new/

Minus a, which is equal to minus dpR.

2. Question 1

Ok, so let's look at this problem. The environment is:

◎ two directories: old, new, old contains three contents: test1 file, test2 directory, and test3, which is a hidden file.


[root@dc5 test]# ll -laR
.:
total 20
drwxr-xr-x 4 root root 4096 Dec 15 11:55 .
drwxrwxrwt 7 root root 4096 Dec 15 11:59 ..
drwxr-xr-x 2 root root 4096 Dec 15 12:14 new
drwxr-xr-x 3 root root 4096 Dec 15 12:14 old

./new:
total 8
drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..

./old:
total 12
drwxr-xr-x 3 root root 4096 Dec 15 12:14 .
drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..
-rw-r--r-- 1 root root  0 Dec 15 12:07 .test3
-rw-r--r-- 1 root root  0 Dec 15 12:05 test1
drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2

./old/test2:
total 8
drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
drwxr-xr-x 3 root root 4096 Dec 15 12:14 ..

◎ operation 1:


[root@dc5 test]# cp -a old/* new/
[root@dc5 test]# ll -laR new/
new/:
total 12
drwxr-xr-x 3 root root 4096 Dec 15 12:15 .
drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..
-rw-r--r-- 1 root root  0 Dec 15 12:05 test1
drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2

new/test2:
total 8
drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
drwxr-xr-x 3 root root 4096 Dec 15 12:15 ..

Here's the problem: the implied.test3 file is not fully copied to the new directory.

The reason: the parameter is not used correctly. This is usually because I am familiar with the Dos format (including myself), but in the bash environment, cp does not match the similar implicit file at the beginning.

When operating 2

The correct way to write it is:


[root@dc5 test]# cp -a old/. new/
[root@dc5 test]# ll -laR new/
new/:
total 12
drwxr-xr-x 3 root root 4096 Dec 15 12:14 .
drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..
-rw-r--r-- 1 root root  0 Dec 15 12:07 .test3
-rw-r--r-- 1 root root  0 Dec 15 12:05 test1
drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2

new/test2:
total 8
drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
drwxr-xr-x 3 root root 4096 Dec 15 12:14 ..

Instead of a * sign, use a. Sign.

There's a more complicated way to write it:


[root@dc5 test]# cp -a old/* old/.[^.]* new/
[root@dc5 test]# ll -laR new/
new/:
total 12
drwxr-xr-x 3 root root 4096 Dec 15 12:25 .
drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..
-rw-r--r-- 1 root root  0 Dec 15 12:07 .test3
-rw-r--r-- 1 root root  0 Dec 15 12:05 test1
drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2

new/test2:
total 8
drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
drwxr-xr-x 3 root root 4096 Dec 15 12:25 ..

Please be careful not to write.*. (see below for reasons)

3. Question 2

It says don't write that. What does that mean?


[root@dc5 test]# echo .*
. ..

.* represents the current directory, as well as the directory at the next level.

So, using.* can lead to bigger problems:


cp -r old/ new/
0

In other words, using dot * is equal to something like this:


cp -r old/ new/
1

Related articles: