coreutils
coreutils copied to clipboard
cp -r behaves differently if target_dir exists or not
Describe the bug
When i use cp -r source-dir target-dir and target-dir does not exist yet, it copies the content (without the source-dir folder) to target-dir.
On the other hand, when target-dir already exists, the result is target-dir/source-dir with the contents.
I am not sure which of the 2 expected behaviors are intended or how to avoid it.
How to reproduce
- Create a directory (named
source-dirfor this test) and place one or more dummy files in it (file1.md,file2.md). - execute
cp -r source-dir target-dir - make sure the files are directly located in the root of
target-dir - execute
cp -r source-dir target-diragain - now the files are in a subfolder of
target-dirnamedsource-dir
Expected behavior
step 4 produces results like in step 2 & 3 as described above
OR
step 2 produces results like step 4 & 5 as described above
Additional Information
output after step 2:
erd target-dir/
0 B ┌─ file1.md
0 B ├─ file2.md
target-dir
2 files
output after step 4:
erd target-dir/
0 B ┌─ file1.md
0 B ├─ file2.md
┌─ source-dir
0 B ├─ file1.md
0 B ├─ file2.md
target-dir
1 directory, 4 files
Run with --debug and -v flag
output of step 2
cp -r source-dir target-dir --debug -v
'/home/roman/source-dir' -> '/home/roman/target-dir/'
'/home/roman/source-dir/file1.md' -> '/home/roman/target-dir/file1.md'
copy offload: unknown, reflink: unsupported, sparse detection: no
'/home/roman/source-dir/file2.md' -> '/home/roman/target-dir/file2.md'
copy offload: unknown, reflink: unsupported, sparse detection: no
output of step 4
cp -r source-dir target-dir --debug -v
'/home/roman/source-dir' -> '/home/roman/target-dir/source-dir'
'/home/roman/source-dir/file1.md' -> '/home/roman/target-dir/source-dir/file1.md'
copy offload: unknown, reflink: unsupported, sparse detection: no
'/home/roman/source-dir/file2.md' -> '/home/roman/target-dir/source-dir/file2.md'
copy offload: unknown, reflink: unsupported, sparse detection: no
Notes
I have also opened an issue at nushell, but i think this is an upstream issue, so i opened this one after some discussion.