问题
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?
回答1:
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?
回答2:
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.
来源:https://stackoverflow.com/questions/54728510/how-to-follow-a-symbolic-soft-link-in-cmd-or-powershell