Could someone explain why this happens?
Most specifically: Why is one 1's content copied to f? And why is f copied to g?
$ tree
.
0 directories, 0 files
$ mkdir 1
$ mkdir 2
$ touch 1/a
$ touch 1/b
$ mkdir 1/c
$ touch 1/c/x
$ tree
.
├── 1
│ ├── a
│ ├── b
│ └── c
│ └── x
└── 2
3 directories, 3 files
$ cp -r 1/* 2/*
zsh: no matches found: 2/*
$ cp -r 1/* 2/*
$ mkdir 2/f
$ mkdir 2/g
$ cp -r 1/* 2/*
$ tree
.
├── 1
│ ├── a
│ ├── b
│ └── c
│ └── x
└── 2
├── f
└── g
├── a
├── b
├── c
│ └── x
└── f
7 directories, 6 files
cp -r 1/* 2/*
in there - it's done twice in a row with (apparently) different results. It should always complain about not finding a2/*
because the shell can't expand that glob - nothing matches it. And the contents of1
is not copied tof
in this example.cp
is kind of ambiguous, you can try--target-directory
, e.g.cp --target-directory=2 fileglob1 fileglob2
I find that option very useful in scripts where I am not sure what globs will work and which ones won't; it helps prevent accidentally overwriting stuff. (Some versions ofcp
allow-t
.)