Execute 'cp -r .*' command with different result between shell and script.

Description

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 # tree -a
.
├── a
│ └── .a
├── b
│ └── .b
├── copy
└── target

# cp -r a/.* target

# tree -a
.
├── a
│ └── .a
├── b
│ └── .b
├── copy
└── target
└── .a

# cat copy
cp -r b/.* target

# ./copy
cp: will not create hard link 'target/b' to directory 'target/.'
cp: cannot copy a directory, 'b/..', into itself, 'target'

# tree -a
.
├── a
│ └── .a
├── b
│ └── .b
├── copy
└── target
├── .a
├── a
│ └── .a
├── .b
└── target
├── .a
└── .b

Reason

cp -r .* command copy include .. and ..

Solution

cp -r `find b -maxdepth 1 -name ".*"` target

Reference

https://stackoverflow.com/questions/246215/how-can-i-generate-a-list-of-files-with-their-absolute-path-in-linux