When I use
cp -R inputFolder outputFolder
the result is context-dependent:
- if
outputFolderdoes not exist, it will be created, and the cloned folder path will beoutputFolder. - if
outputFolderexists, then the clone created will beoutputFolder/inputFolder
This 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
cpto be portable (e.g. MINGW does not haversyncshipped) - I checked
cp -R --parentsbut this recreates the path all the way up the directory tree (so the clone will not beoutputFolderbutsome/path/outputFolder) --remove-destinationor--updatein case 2 do not change anything, still things are copied intooutputFolder/inputFolder
Is 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?
mvfrom moving a collection of files into a single regular one?rsync?cp, piping between twotarcommands is a nice reliable way to copy trees.rsyncshipped.tar -C input -cf - . | tar -C output -xf -works.-Cchanges working directory, but it is not a standard option, so if it's not supported you need to run the two tars in the right working directories to begin with, e.g.( cd input && tar -cf - . ) | ( cd output && tar -xf - )However if you're dealing with Windows I would just use roaima's answer.