Is there Windows copy command that mimic Unix `cp -r` behavior?

Posted on

QUESTION :

I don’t know how to name this behavior vividly, just call it loyalcopy.

Two figures will illustrate it clearly.

before copy

I’d like to issue two commands to copy two filesystem nodes (regardless it is a file for a directory):

loyalcopy c:libsrcinclude     d:tempdest
loyalcopy c:libsrcmm_psfunc.h d:tempdest

and see result like this:

after copy

What actual Windows commands can achieve this effect? I tried xcopy and robocopy, and they do not work as desired. For example, xcopy c:libsrcinclude d:tempdest /s would copy contents inside include, not creating a mirrored include directory in destination folder.

On Unix, I know I can replace loyalcopy with cp -r to achieve desired result. But is there Windows equivalent of it? Better to have Windows stock commands, no third-party software involved.

ANSWER :

Just run cp -r c:libsrcinclude d:tempdest in PowerShell. It’ll work as expected. It’s actually the alias to

Copy-Item -Recurse c:libsrcinclude d:tempdest

With xcopy or robocopy you’ll need to specify the folder name in the destination in order for it to be created

robocopy /E c:libsrcinclude d:tempdestinclude
xcopy /E /I /H c:libsrcinclude d:tempdestinclude

/I: If Source is a directory or contains wildcards and Destination does not exist, xcopy assumes Destination specifies a directory name and creates a new directory. Then, xcopy copies all specified files into the new directory. By default, xcopy prompts you to specify whether Destination is a file or a directory.

https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy

To get help on any classic Windows tools just use /?, for example xcopy /?, robocopy /?. Note that xcopy has been deprecated and may be removed in the future

Try this:

xcopy /h /e /i "c:libsrcinclude" ""d:tempdestinclude""

Leave a Reply

Your email address will not be published. Required fields are marked *