When I use cp -R inputFolder outputFolderthe result is context-dependent:if outputFolder does not exist, it will be created, and the cloned folder path will be outputFolder.if outputFolder exists, then the clone created will be outputFolder/inputFolderThis is horrible, because I want to create some installation script, and if the user runs it twice by mistake, he will have outputFolder created the first time, then on the second run all the stuff will be created once again in outputFolder/inputFolder.I want always the first behavior: create a clone next to the original (as a sibling).I want to use cp to be portable (e.g. MINGW does not have rsync shipped)I checked cp -R --parents but this recreates the path all the way up the directory tree (so the clone will not be outputFolder but some/path/outputFolder)--remove-destination or --update in case 2 do not change anything, still things are copied into outputFolder/inputFolderIs there a way to do this without first checking for existence of the outputFolder (if folder does not exist then...) or using rm -rf outputFolder?What is the agreed, portable UNIX way of doing that?
How to copy a folder recursively in an idempotent way using cp?
cp;recursive
Use this instead:cp -R inputFolder/. outputFolderThis 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: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.ccc does exist:The command cp -R aaa/. ccc means copy the contents of aaa/. into ccc/., i.e. copy aaa into ccc.