Skip to main content
Removed nonstandard alternative
Source Link
Chris Davies
  • 128k
  • 16
  • 178
  • 323

Use this instead:

cp -R inputFolder/. outputFolder

This works in exactly the same way that, say, cp -R aaa/bbb ccc works: if ccc doesn't exist then it's created as a copy of bbb and its contents; but if ccc already exists then ccc/bbb is created as the copy of bbb and its contents.

For almost any instance of bbb this gives the undesirable behaviour that you noted in your Question. However, in this specific situation the bbb is just ., so aaa/bbb is really aaa/., which in turn is really just aaa but by another name. So we have these two scenarios:

  1. ccc does not exist:

    The command cp -R aaa/. ccc means "create ccc and copy the contents of aaa/. into ccc/., i.e. copy aaa into ccc.

  2. ccc does exist:

    The command cp -R aaa/. ccc means "copy the contents of aaa/. into ccc/., i.e. copy aaa into ccc.

You can also shorten the command marginally by removing the trailing . from the source and leaving it implied by the trailing directory separator (but I don't know how standard this is):

cp -R inputFolder/ outputFolder

Use this instead:

cp -R inputFolder/. outputFolder

This works in exactly the same way that, say, cp -R aaa/bbb ccc works: if ccc doesn't exist then it's created as a copy of bbb and its contents; but if ccc already exists then ccc/bbb is created as the copy of bbb and its contents.

For almost any instance of bbb this gives the undesirable behaviour that you noted in your Question. However, in this specific situation the bbb is just ., so aaa/bbb is really aaa/., which in turn is really just aaa but by another name. So we have these two scenarios:

  1. ccc does not exist:

    The command cp -R aaa/. ccc means "create ccc and copy the contents of aaa/. into ccc/., i.e. copy aaa into ccc.

  2. ccc does exist:

    The command cp -R aaa/. ccc means "copy the contents of aaa/. into ccc/., i.e. copy aaa into ccc.

You can also shorten the command marginally by removing the trailing . from the source and leaving it implied by the trailing directory separator (but I don't know how standard this is):

cp -R inputFolder/ outputFolder

Use this instead:

cp -R inputFolder/. outputFolder

This works in exactly the same way that, say, cp -R aaa/bbb ccc works: if ccc doesn't exist then it's created as a copy of bbb and its contents; but if ccc already exists then ccc/bbb is created as the copy of bbb and its contents.

For almost any instance of bbb this gives the undesirable behaviour that you noted in your Question. However, in this specific situation the bbb is just ., so aaa/bbb is really aaa/., which in turn is really just aaa but by another name. So we have these two scenarios:

  1. ccc does not exist:

    The command cp -R aaa/. ccc means "create ccc and copy the contents of aaa/. into ccc/., i.e. copy aaa into ccc.

  2. ccc does exist:

    The command cp -R aaa/. ccc means "copy the contents of aaa/. into ccc/., i.e. copy aaa into ccc.

Added an explanation
Source Link
Chris Davies
  • 128k
  • 16
  • 178
  • 323

Use this instead:

cp -R inputFolder/. outputFolder

This works in exactly the same way that, say, cp -R aaa/bbb ccc works: if ccc doesn't exist then it's created as a copy of bbb and its contents; but if ccc already exists then ccc/bbb is created as the copy of bbb and its contents.

For almost any instance of bbb this gives the undesirable behaviour that you noted in your Question. However, in this specific situation the bbb is just ., so aaa/bbb is really aaa/., which in turn is really just aaa but by another name. So we have these two scenarios:

  1. ccc does not exist:

    The command cp -R aaa/. ccc means "create ccc and copy the contents of aaa/. into ccc/., i.e. copy aaa into ccc.

  2. ccc does exist:

    The command cp -R aaa/. ccc means "copy the contents of aaa/. into ccc/., i.e. copy aaa into ccc.

You can also shorten the command marginally by removing the trailing . from the source and leaving it implied by the trailing directory separator (but I don't know how standard this is):

cp -R inputFolder/ outputFolder

Use this instead

cp -R inputFolder/. outputFolder

Use this instead:

cp -R inputFolder/. outputFolder

This works in exactly the same way that, say, cp -R aaa/bbb ccc works: if ccc doesn't exist then it's created as a copy of bbb and its contents; but if ccc already exists then ccc/bbb is created as the copy of bbb and its contents.

For almost any instance of bbb this gives the undesirable behaviour that you noted in your Question. However, in this specific situation the bbb is just ., so aaa/bbb is really aaa/., which in turn is really just aaa but by another name. So we have these two scenarios:

  1. ccc does not exist:

    The command cp -R aaa/. ccc means "create ccc and copy the contents of aaa/. into ccc/., i.e. copy aaa into ccc.

  2. ccc does exist:

    The command cp -R aaa/. ccc means "copy the contents of aaa/. into ccc/., i.e. copy aaa into ccc.

You can also shorten the command marginally by removing the trailing . from the source and leaving it implied by the trailing directory separator (but I don't know how standard this is):

cp -R inputFolder/ outputFolder
Source Link
Chris Davies
  • 128k
  • 16
  • 178
  • 323

Use this instead

cp -R inputFolder/. outputFolder