如何在 cmd 或 PowerShell 中跟踪符号/软链接?

Deo*_*xal 4 windows powershell symlink cmd desktop-shortcut

我的搜索只向我展示了如何在 cmd 中使用mklink创建符号链接。我看到一些东西说要使用readlink,但是 PowerShell 和 cmd 不知道什么是readlink,而且cd显然不起作用。那么我如何跟随一个?

mkl*_*nt0 9

为避免您的问题引起混淆:

  • Windows快捷方式文件( *.lnkfiles)Windows (GUI) shell 的一个功能,不同于符号链接( symlinks ),后者是(NTFS)文件系统的一个功能

  • 快捷方式文件- 您感兴趣的 - 存储它们指向文件内的文件或文件夹的路径,这就是为什么:

    • 不能直接cd访问快捷方式文件的目标文件夹,因为诸如文件系统命令对文件内容cd一无所知。
    • 必须阅读快捷方式文件的内容以确定其 target,然后您可以将其传递给cdor Set-Location(在 PowerShell 中)。
    • 快捷方式文件的文件格式是二进制文件格式,可以通过公开 Windows shell 功能的内置 COM 组件读取;例如,要确定命名的快捷方式文件的目标文件夹Samples.lnk并将其更改为该文件夹,请使用 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
      
      Run Code Online (Sandbox Code Playgroud)
  • 符号链接,相比之下:

    • (通常)透明地重定向到它们的target,即它们指向的文件系统项(文件或文件夹)。

    • 你可以因此使用cd同一个符号链接到一个文件夹直接,但要注意,它仍然是符号链接的路径显示。

    • 打印符号链接的目标- 类似于该readlink实用程序在类 Unix 平台上所做的工作- 使用 PowerShell;例如,打印Samples当前目录中命名的符号链接的目标:

       (Get-Item Samples).Target
      
       # Or, after running `cd Samples`:
       (Get-Item .).Target
      
      Run Code Online (Sandbox Code Playgroud)
    • 请注意,在 中获取符号链接的目标并不简单cmd.exe,但如果您使用
      dir /al <link-path>*,则列表还将显示链接的目标路径,在名称之后,用[...];括起来。请注意,尾随*是必要的,以便显示有关链接本身的信息,而不是其目标的内容;请注意,虽然不太可能,但它也可能与以相同路径开头的其他链接匹配。


与快捷方式文件不同,符号链接在 Windows 世界中仍然很少见,尤其是因为在 Windows 10 之前,它们总是需要管理员权限才能创建;在 Windows 10 中,如果启用了开发者模式(由管理员),即使是非管理用户/非高级进程现在也可以创建符号链接 - 请参阅https://blogs.windows.com/buildingapps/2016/12/02/symlinks -windows-10/,这也解释了为什么符号链接在未来的使用率可能会增加


bdn*_*n02 5

对于您的问题,我制作了这个批处理文件:

mkdir truedir
dir > truedir\fileone.txt
mklink /d symdir truedir
cd symdir
dir
Run Code Online (Sandbox Code Playgroud)

而且我发现从命令提示符获取到目录的符号链接的内容没有问题。powershell 5.1(win 10)也没有问题:

Get-ChildItem C:\Users\<user>\OneDrive\Desktop\test2\symdir
Run Code Online (Sandbox Code Playgroud)

你能给我们一个代码示例(批处理或powershell是一样的)来复制你的问题吗?