When I use
cp -R inputFolder outputFolder
the result is context-dependent:
- if
outputFolder
does not exist, it will be created, and the cloned folder path will beoutputFolder
. - if
outputFolder
exists, 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
cp
to be portable (e.g. MINGW does not haversync
shipped) - I checked
cp -R --parents
but this recreates the path all the way up the directory tree (so the clone will not beoutputFolder
butsome/path/outputFolder
) --remove-destination
or--update
in 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?
mv
from moving a collection of files into a single regular one?rsync
?cp
, piping between twotar
commands is a nice reliable way to copy trees.rsync
shipped.tar -C input -cf - . | tar -C output -xf -
works.-C
changes 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.