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 PowerShell and cmd don't know what readlink is, and cd obviously doesn't work. So how do I follow one?
For your question i made this batch file:
mkdir truedir
dir > truedir\fileone.txt
mklink /d symdir truedir
cd symdir
dir
And i have found no problem to get the content of the symblic link to a directory from command prompt. No problem also with powershell 5.1 (win 10):
Get-ChildItem C:\Users\<user>\OneDrive\Desktop\test2\symdir
Can you give us a code example (batch or powershell is the same) to replicate your problem?
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:
- You cannot directly
cd
to a shortcut file's target folder, because filesystem commands such ascd
know nothing about the content of files. - You must read the content of the shortcut file to determine its target, which you can then pass to
cd
orSet-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
- You cannot directly
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 namedSamples
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 usedir /al <link-path>*
, 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.
.Target
property of what Get-ChildItem
/ Get-Item
returns (this was removed from PowerShell Core). –
Andresandresen For your question i made this batch file:
mkdir truedir
dir > truedir\fileone.txt
mklink /d symdir truedir
cd symdir
dir
And i have found no problem to get the content of the symblic link to a directory from command prompt. No problem also with powershell 5.1 (win 10):
Get-ChildItem C:\Users\<user>\OneDrive\Desktop\test2\symdir
Can you give us a code example (batch or powershell is the same) to replicate your problem?
Okay I just posted this answer in another similar question so I figured I'd post it here too. I wish this one had come up in a search like the other question did because @mklement0's answer is great. But here is my solution that is able to follow nested links to the true file or directory in PowerShell (I was looking for a PS-specific solution, not CMD).
So far the double-cd approach is the most reliable/universal I can find for handling a situation of mixed relative/absolute paths in the filenames and targets, and I tested several scenarios.
function getLinkTarget($fn) {
$op=$PWD #Save original path
while($t=(Get-Item $fn).Target) { #Get link target
cd (Split-Path -Parent $fn) #cd to parent of file/dir
cd (Split-Path -Parent $t) #cd again to parent of target
$fn=(Split-Path -Leaf $t) #Set filename to relative target
}
$fn=(Join-Path $PWD $fn) #Make path absolute
cd $op #Change back to original path
return $fn
}
© 2022 - 2024 — McMap. All rights reserved.