0

To copy the contents of one directory into another, I can use the following:

cp -Rip source-dir/ ../destination-dir/

However, it seems that using the trailing slash at the end of source directory when using cp and mv commands is somewhat discouraged.

No trailing slash on source directory

You should not put a trailing slash on the source directory:

The point is relevant to cp - but also to mv, where it is much more important.

I'll cite the warning from the manual - note that it's not found in the man page, but in the info page info coreutils 'mv invocation':

Warning: Avoid specifying a source name with a trailing slash, when it might be a symlink to a directory. Otherwise, 'mv' may do something very surprising, since its behavior depends on the underlying rename system call. On a system with a modern Linux-based kernel, it fails with 'errno=ENOTDIR'. However, on other systems (at least FreeBSD 6.1 and Solaris 10) it silently renames not the symlink but rather the directory referenced by the symlink.

Is it really so? And if the answer is "yes", what is the recommended way to copy the contents of a directory?

jsx97
  • 305

1 Answers1

1

I may be completely missing the aim of the question, but couldn't you just use globbing?

For example: cp -Rip source_dir/* ../destination_dir

The * after the trailing slash would copy the directory structure recursively without copying the directory itself.

❯ lt *_dir

destination_dir:

source_dir:

└──  testdir/ │ ├────  test_1_1.txt │ ├────  test_1_2.txt │ └────  test_1_3.txt ├──  test_1.txt ├──  test_2.txt └──  test_3.txt

Found 7 items in total.

Folders         : 1
Recognized files    : 6
Unrecognized files  : 0

❯ cp -Rip source_dir/* destination_dir ❯ lt *_dir

destination_dir:

└──  testdir/ │ ├────  test_1_1.txt │ ├────  test_1_2.txt │ └────  test_1_3.txt ├──  test_1.txt ├──  test_2.txt └──  test_3.txt

source_dir:

└──  testdir/ │ ├────  test_1_1.txt │ ├────  test_1_2.txt │ └────  test_1_3.txt ├──  test_1.txt ├──  test_2.txt └──  test_3.txt

Found 14 items in total.

Folders         : 2
Recognized files    : 12
Unrecognized files  : 0

rdbreak
  • 11