Skip to main content
added 198 characters in body
Source Link
Kaz
  • 8.9k
  • 2
  • 31
  • 52

The problem is most certainly not that cp doesn't copy directory structures. Of course it does: cp -r (recursive).

What cp, at least POSIX cp, doesn't do is re-create the relative paths of the source operands in the destination directory.

If you cp a/b/file1 a/b/file2 a/c/file3 dest then you get dest/file1 dest/file2 dest/file3.

If your cp is the one from GNU Coreutils, it may have the --parents option. Not sure when that was introduced. cp --parents will induce GNU cp into producing this path re-creation mechanism. From the Info documentation:

--parents
  Form the name of each destination file by appending to the target
  directory a slash and the specified name of the source file.  The
  last argument given to `cp' must be the name of an existing
  directory.  For example, the command:

      cp --parents a/b/c existing_dir

  copies the file `a/b/c' to `existing_dir/a/b/c', creating any
  missing intermediate directories.

So if you have GNU cp, the only change you need is to drop the -r since we are not recursing, and use --parents:

cp --parents -f `head -n 100 /home/tmp/abc.txt` /home/tmp/test/files

The problem is most certainly not that cp doesn't copy directory structures. Of course it does: cp -r (recursive).

What cp, at least POSIX cp, doesn't do is re-create the relative paths of the source operands in the destination directory.

If you cp a/b/file1 a/b/file2 a/c/file3 dest then you get dest/file1 dest/file2 dest/file3.

If your cp is the one from GNU Coreutils, it may have the --parents option. Not sure when that was introduced. cp --parents will induce GNU cp into producing this path re-creation mechanism. From the Info documentation:

--parents
  Form the name of each destination file by appending to the target
  directory a slash and the specified name of the source file.  The
  last argument given to `cp' must be the name of an existing
  directory.  For example, the command:

      cp --parents a/b/c existing_dir

  copies the file `a/b/c' to `existing_dir/a/b/c', creating any
  missing intermediate directories.

The problem is most certainly not that cp doesn't copy directory structures. Of course it does: cp -r (recursive).

What cp, at least POSIX cp, doesn't do is re-create the relative paths of the source operands in the destination directory.

If you cp a/b/file1 a/b/file2 a/c/file3 dest then you get dest/file1 dest/file2 dest/file3.

If your cp is the one from GNU Coreutils, it may have the --parents option. Not sure when that was introduced. cp --parents will induce GNU cp into producing this path re-creation mechanism. From the Info documentation:

--parents
  Form the name of each destination file by appending to the target
  directory a slash and the specified name of the source file.  The
  last argument given to `cp' must be the name of an existing
  directory.  For example, the command:

      cp --parents a/b/c existing_dir

  copies the file `a/b/c' to `existing_dir/a/b/c', creating any
  missing intermediate directories.

So if you have GNU cp, the only change you need is to drop the -r since we are not recursing, and use --parents:

cp --parents -f `head -n 100 /home/tmp/abc.txt` /home/tmp/test/files
Source Link
Kaz
  • 8.9k
  • 2
  • 31
  • 52

The problem is most certainly not that cp doesn't copy directory structures. Of course it does: cp -r (recursive).

What cp, at least POSIX cp, doesn't do is re-create the relative paths of the source operands in the destination directory.

If you cp a/b/file1 a/b/file2 a/c/file3 dest then you get dest/file1 dest/file2 dest/file3.

If your cp is the one from GNU Coreutils, it may have the --parents option. Not sure when that was introduced. cp --parents will induce GNU cp into producing this path re-creation mechanism. From the Info documentation:

--parents
  Form the name of each destination file by appending to the target
  directory a slash and the specified name of the source file.  The
  last argument given to `cp' must be the name of an existing
  directory.  For example, the command:

      cp --parents a/b/c existing_dir

  copies the file `a/b/c' to `existing_dir/a/b/c', creating any
  missing intermediate directories.