Skip to main content
3 of 3
Removed nonstandard alternative
Chris Davies
  • 128.3k
  • 16
  • 179
  • 324

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.

Chris Davies
  • 128.3k
  • 16
  • 179
  • 324