coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

cp -r behaves differently if target_dir exists or not

Open therealchfkch opened this issue 1 year ago • 6 comments

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

  1. Create a directory (named source-dir for this test) and place one or more dummy files in it (file1.md, file2.md).
  2. execute cp -r source-dir target-dir
  3. make sure the files are directly located in the root of target-dir
  4. execute cp -r source-dir target-dir again
  5. now the files are in a subfolder of target-dir named source-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.

therealchfkch avatar Aug 28 '24 13:08 therealchfkch