windows symbolic link target
Asked Answered
B

7

39

Say that I set up a symbolic link:

mklink  /D C:\root\Public\mytextfile.txt C:\root\Public\myothertextfile.txt

Editor's note: Option /D - which is for creating directory symlinks - is at odds with targeting files, as in this example, which has caused some confusion. To create a file symlink, simply omit /D.

Is there a way to see what the target of mytextfile.txt is, using the command line?

Balsamic answered 17/4, 2012 at 9:19 Comment(3)
or if possible how do i chase the link in perlBalsamic
It's a bit odd to have a directory named mytextfile.txt; are you sure you meant to use the /D option?Willardwillcox
Anyway, the "dir" command shows the target of symbolic links by default.Willardwillcox
W
29

As Harry Johnston said dir command shows the target of symbolic links

2014/07/31  11:22    <DIR>          libs
2014/08/01  13:53             4,997 mobile.iml
2014/07/31  11:22               689 proguard-rules.pro
2014/09/28  10:54    <JUNCTION>     res [\??\C:\Users\_____\mobile\src\main\res]
Weight answered 28/9, 2014 at 3:47 Comment(3)
That's actually a junction point, not a symbolic link, but a directory listing for a symbolic link looks very similar.Willardwillcox
When using git bash, dir is not available, but you can write instead cmd //C dirDramatist
Also, when using Powershell, dir may be an alias. Like the comment about git bash, use cmd /C dir to get the expected output.Galbanum
P
19

To complement Paul Verest's helpful answer:

  • While cmd's dir indeed shows the link type and target path, extracting that information is nontrivial - see below for a PowerShell alternative.

  • In order to discover all links in the current directory, use dir /aL.

PowerShell (PSv5+) solutions:

List all links and their targets in the current directory as full paths:

PS> Get-ChildItem | ? Target | Select-Object FullName, Target

FullName                      Target
--------                      ------
C:\root\public\mytextfile.txt {C:\root\Public\myothertextfile.txt}

Determine a given link's target:

PS> (Get-Item C:\root\Public\mytextfile.txt).Target
C:\root\Public\myothertextfile.txt
Pomiferous answered 23/3, 2018 at 22:32 Comment(1)
Get-ChildItem | ? Target | Select-Object FullName, Target | Format-List if the path is getting cut off with ...Jacquejacquelin
F
5

Write a batch file( get_target.bat) to get the target of the symbolic link:

@echo off
for /f "tokens=2 delims=[]" %%H in  ('dir /al %1 ^| findstr /i /c:"%2"') do (
    echo %%H
)

For example, to get the target of C:\root\Public\mytextfile.txt:

get_target.bat C:\root\Public\ mytextfile.txt
Fullbodied answered 16/4, 2019 at 12:23 Comment(1)
^ is Escape character. Adding the escape character before a command symbol allows it to be treated as ordinary text. When piping or redirecting any of these characters you should prefix with the escape character: & \ < > ^ |Fullbodied
M
1

Maybe someone's interested in a C# method to resolve all directory symlinks in a directory similar to Directory.GetDirectories(). To list Junctions or File symlinks, simply change the regex.

static IEnumerable<Symlink> GetAllSymLinks(string workingdir)
{
    Process converter = new Process();
    converter.StartInfo = new ProcessStartInfo("cmd", "/c dir /Al") { RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = workingdir };
    string output = "";
    converter.OutputDataReceived += (sender, e) =>
    {
        output += e.Data + "\r\n";
    };
    converter.Start();
    converter.BeginOutputReadLine();
    converter.WaitForExit();

    Regex regex = new Regex(@"\n.*\<SYMLINKD\>\s(.*)\s\[(.*)\]\r");

    var matches = regex.Matches(output);
    foreach (Match match in matches)
    {
        var name = match.Groups[1].Value.Trim();
        var target = match.Groups[2].Value.Trim();
        Console.WriteLine("Symlink: " + name + " --> " + target);

        yield return new Symlink() { Name = name, Target = target };
    }
}

class Symlink
{
    public string Name { get; set; }
    public string Target { get; set; }
}
Maggy answered 13/10, 2015 at 14:49 Comment(0)
M
0

all credits to @SecurityAndPrivacyGuru , [cmd]

complete batch script/function that reads symlink{|s in folder} and outputs list with them and their target paths

@echo off
setlocal enableExtensions enableDelayedExpansion
cd /D "%~dp0"
set br=^


rem br;


set "pafIf=<<pafToSymlink|pafToFolder>>"
set "gIfZsymLink="
for /f "tokens=*" %%q in ('dir "!pafIf!" /al /b') do (
    for /f "tokens=2 delims=[]" %%r in ('dir /al ^| findstr /i /c:"%%q"') do (
        set "gIfZsymLink=!gIfZsymLink!%%~fq>%%r!br!"
    )
)
set "gIfZsymLink=!gIfZsymLink:~0,-1!"
rem echo "!gIfZsymLink!"

for /f "tokens=1,2 delims=>" %%q in ("!gIfZsymLink!") do (
    echo symlink: %%q , filepath: %%r
)


:scIn
rem endlocal
pause
rem exit /b
Mezereum answered 2/4, 2019 at 19:13 Comment(0)
H
0

Command Line options

SuperUser Julian Knight's answer lists some resources to Windows equivalents for Linux commands. The most relevant command for this would be ln.exe. Here are links to download, and how to use these utilities.

Using Windows GUI

Alternatively, if you want to see the target of symlinks using the Windows GUI interface (specifically, the file Properties window), install the Windows Link Shell Extension (direct link to the file download):

SuperUser Julian Knight's answer worked well for me.

With Windows Link Shell Extension installed, you can right-click on the link in Windows Explorer and check the properties. There is a tab that allows you to change the link directly.

With Link Shell Extension installed:

  • When you right-click a file to open up "Properties" window, it includes an additional tab: "Link Properties", showing the Target of symlinks.
  • You can also edit this Target field to change the symlink's target.
  • And since the field is editable, copy-paste of the target link is easy.
  • As is scrolling to see the entire path of the target file!
    (In contrast, the "Location" field elsewhere in the Properties window does NOT allow scrolling to see the full path of the file you're examining.)

BTW

The WAMP server icon no longer appeared in the system tray (noticed) shortly after installing Link Shell Extension, which means that while WAMPP still runs (local websites work as expected), none of the WAMP functions could be accessed.
It is not definitive that the install caused WAMPP tray icon to disappear, but the Good news is that after a reboot, the WAMPP re-appeared in the system tray, as normal. :-)

Homey answered 6/10, 2020 at 16:15 Comment(0)
T
0

Addendum to accepted answer, viewing symlinks in Windows GUI:

  • folder symlinks are labeled as File folder
  • adding the Atttributes column helps differentiate
  • the Size column reports inconsistently: only shortcuts report actual size including for folders

Each of the files and folders below contain the same 2KB data.

windows gui symlinks

Thirlage answered 16/12, 2022 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.