My searches have only shown me how to create symbolic links using mklink in cmd. I have seen some things saying to use readlink, but PowerS
To avoid confusion stemming from your question:
Windows shortcut files (*.lnk files), which are a feature of the Windows (GUI) shell, are distinct from symbolic links (symlinks), which are a feature of the (NTFS) filesystem.
Shortcut files - which you are interested in - store the path of a file or folder they point to inside the file, which is why:
cd
to a shortcut file's target folder, because filesystem commands such as cd
know nothing about the content of files.cd
or Set-Location
(in PowerShell).The file format of shortcut file is a binary one that can be read via an in-box COM component that exposes Windows shell functionality; e.g., to determine the target folder of a shortcut file named Samples.lnk
and change to that folder, use PowerShell:
# NOTE: * Despite the name "CreateShortcut()", the method is also
# used to *read* shortcut files.
# * Prefixing the filename with "$PWD/" is needed in order
# to target a file in the current directory, because
# the method doesn't know what PowerShell's current dir. is.
cd (New-Object -ComObject WScript.Shell).CreateShortcut("$PWD/Samples.lnk").TargetPath
Symlinks, by contrast:
(typically) transparently redirect to their target, i.e., the filesystem item (file or folder) they point to.
You can therefore use cd
directly with a symlink to a folder, but note that it is still the symlink's path that is shown.
To print a symlink's target - akin to what the readlink
utility does on Unix-like platforms - use PowerShell; e.g., to print the target of a symlink named Samples
in the current directory:
(Get-Item Samples).Target
# Or, after running `cd Samples`:
(Get-Item .).Target
Note that it's not straightforward to get a symlink's target in cmd.exe
, but if you use dir /al
, the listing will also show the link's target path, after the name, enclosed in [...]
; note that the trailing *
is necessary in order to show information about the link itself, not its target's contents; note that, although unlikely, that may match other links that start with the same path as well.
Unlike shortcut files, symlinks are still rare in the Windows world, not least because prior to Windows 10 they invariably required admin privileges to create; in Windows 10, if developer mode is enabled (by an administrator), even non-administrative users / non-elevated processes can now create symlinks - see https://blogs.windows.com/buildingapps/2016/12/02/symlinks-windows-10/, which also explains why symlinks are likely to see increasing usage in the future.