How to copy the target file instead the symlink

Jose Sánchez 20 Reputation points
2024-10-04T10:51:14.73+00:00

Hi, I’m trying to copy a directory tree that contains many symlink files. But I need to copy the target of the symlink, not the symlink.

I tried with Windows 10 and 11, and with xcopy and robocopy, but it always copy the symlink, not the linked file, in spite that in theory according to the description of those commands they should copy the linked file by default.

The symlink file (.lnk) was created using the Windows File Explorer.

Thanks for your help.

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,575 questions
0 comments No comments
{count} votes

Accepted answer
  1. Marcin Policht 23,700 Reputation points MVP
    2024-10-04T11:30:36.26+00:00

    When using Windows 10/11 and trying to copy a directory tree with symlinks, tools like xcopy and robocopy indeed do not handle symlinks the same way Linux does, where you'd expect them to follow the symlink and copy the target file by default. It seems that Windows tools, even though they provide options, don't always behave as expected with .lnk files (which are more like Windows shortcuts, not traditional symlinks).

    Here are some options you can try:

    1. Using Robocopy with /SL Option

    Though you mentioned trying robocopy, ensure you are using the /SL switch, which specifies that robocopy should copy the symbolic link itself and not the target.

    Here's a typical robocopy command:

    
    robocopy C:\source\path C:\destination\path /E /COPYALL
    

    This command should, in theory, copy all file attributes and the target of symlinks (when not using /SL). However, .lnk files created by Windows Explorer might still behave differently.

    1. Use a Third-Party Tool

    If the above methods do not work as expected, consider using third-party tools that handle symbolic links more reliably. Tools like Cygwin or rsync (for Windows) can better handle file operations similar to how Linux does.

    For example, you can install Cygwin or Git Bash (which includes rsync), and use:

    rsync -av --copy-links /cygdrive/c/source/path /cygdrive/c/destination/path
    

    This --copy-links option will ensure that symlink targets are copied rather than the symlinks themselves.

    1. Convert .lnk Files to NTFS Symlinks

    Windows .lnk files are technically shortcuts, not true symbolic links in the Unix/Linux sense. If you have access to mklink, you can try converting .lnk files to actual NTFS symlinks using:

    mklink /D linkName targetDirectory
    

    Once converted to NTFS symlinks, the native tools like robocopy should handle them as you intend.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.