我的搜索只告诉我如何在cmd中使用mklink创建符号链接。我看到有些东西说要使用readlink,但PowerShell和cmd不知道readlink是什么,而cd显然也不起作用。那么我该怎么跟随一个符号链接呢?
我的搜索只告诉我如何在cmd中使用mklink创建符号链接。我看到有些东西说要使用readlink,但PowerShell和cmd不知道readlink是什么,而cd显然也不起作用。那么我该怎么跟随一个符号链接呢?
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 <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.
与快捷方式不同,符号链接在Windows世界中仍然很少见,这主要是因为在Windows 10之前,它们无论如何都需要管理员权限才能创建;在Windows 10中,如果开启开发者模式(由管理员),即使是非管理员用户/非提升进程现在也可以创建符号链接-请参见https://blogs.windows.com/buildingapps/2016/12/02/symlinks-windows-10/,其中还解释了为什么符号链接未来可能会越来越常见。
针对您的问题,我制作了这个批处理文件:
mkdir truedir
dir > truedir\fileone.txt
mklink /d symdir truedir
cd symdir
dir
我发现使用命令提示符获取符号链接到目录的内容没有问题。 在PowerShell 5.1(win 10)中也没有问题:
Get-ChildItem C:\Users\<user>\OneDrive\Desktop\test2\symdir
好的,我刚刚在另一个类似的问题中发布了这个答案,所以我觉得我也应该在这里发布。我希望这个问题能像其他问题一样在搜索中出现,因为@mklement0的答案很棒。但这是我的解决方案,它能够在PowerShell中跟随嵌套链接到真正的文件或目录(我正在寻找PS特定的解决方案,而不是CMD)。
到目前为止,双cd方法是我能找到的处理文件名和目标中混合相对/绝对路径情况最可靠/通用的方法,并且我测试了几种情况。
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
}
Get-ChildItem
/Get-Item
返回的.Target
属性(这个属性已从 PowerShell Core 中移除)。 - mklement0