简体   繁体   English

如何将符号链接文件从Linux复制到Windows,然后再复制到Linux,但仍将其保留为符号链接

[英]How to copy symbolic link file from Linux to Windows and then back to Linux but still keeping it as a symbolic link

I have a symbolic link in my Linux machine. 我的Linux机器上有一个符号链接。

I want to copy just the symbolic link (not the target) to a Windows machine and then copy this symbolic link from Windows machine back to some other Linux machine and symbolic link should continue to work. 我想只将符号链接(不是目标)复制到Windows机器,然后将此符号链接从Windows机器复制回其他Linux机器,符号链接应继续工作。

What I tried: 我尝试了什么:

  1. I gziped the symbolic link 我gziped符号链接
  2. Moved gzipped file to Windows machine using WinSCP 使用WinSCP将gzip压缩文件移动到Windows计算机
  3. Extracted symbolic link 提取的符号链接
  4. Moved symbolic file to Linux machine using WinSCP 使用WinSCP将符号文件移动到Linux机器

Now this file is not a symbolic link anymore. 现在这个文件不再是符号链接了。

Do anybody knows a trick to do this? 有人知道这样做的诀窍吗?

In *nix a symlink is typically just a plain text file with a "symlink" attribute. 在* nix中,符号链接通常只是具有“symlink”属性的纯文本文件。 The file contains the path to the link target. 该文件包含链接目标的路径。 The "symlink" attribute does not exist on Windows. Windows上不存在“symlink”属性。 So when you extract the symlink on Windows, it becomes a regular text file [though it may also error, it may depend on a tool you use to extract the archive]. 因此,当您在Windows上提取符号链接时,它将成为常规文本文件[虽然它也可能出错,但它可能取决于您用来提取存档的工具]。 When copied back to *nix, it stays a regular text file. 复制回* nix时,它将保留常规文本文件。

The only solution would be to keep the "symlink" attribute in some external metadata store and restore the attribute when uploading the file or creating the archive. 唯一的解决方案是在某些外部元数据存储中保留“symlink”属性,并在上载文件或创建存档时恢复该属性。

Though I'm not aware of any tool that supports this. 虽然我不知道任何支持这个的工具。

You can definitely code this. 你绝对可以编码。

  1. Using WinSCP : You make a code that generates WinSCP script . 使用WinSCP :您创建一个生成WinSCP脚本的代码。 The code would recursively iterate a local directory structure. 代码将递归迭代本地目录结构。 For a file it will generate the put command to upload it. 对于文件,它将生成put命令以上载它。 For a symlink it will generate the ln command to create a symlink. 对于符号链接,它将生成ln命令以创建符号链接。 To distinguish the symlink, you can maybe use just a simple heuristics (symlink = a short one-line text file with slashes). 为了区分符号链接,您可以使用一个简单的启发式方法(符号链接=带有斜杠的简短单行文本文件)。 A proper way would be to remember the file symlink attribute when extracting the archive (but you would have to code the extraction yourself too then, see also a hint below). 一种正确的方法是在提取存档时记住文件符号链接属性(但是你也必须自己编写提取代码,请参阅下面的提示)。

  2. Using archive : I recently implemented this for a ZIP archive. 使用存档 :我最近为ZIP存档实现了这个。 (Even on Windows) You can use the PHP method ZipArchive::setExternalAttributes to flag an archived file as a symlink. (即使在Windows上)您可以使用PHP方法ZipArchive::setExternalAttributes将归档文件标记为符号链接。 Note that the function is available since PHP 5.6 only. 请注意,该功能仅适用于PHP 5.6。

    Sample code: 示例代码:

     $symlink = true; // is symlink? $dir = false; // is folder? $mode = "755"; // permissions $local_path = "C:\\\\zip\\\\folder\\\\mylink"; $zip_path = "folder/mylink"; $attr = (1 << 14) | // this bit seems to be always set (1 << ($dir ? 30 : 31)) | ($symlink ? (1 << 29) : 0) | octdec($mode) << 16; $zip->addFile($local_path, $zip_path); $zip->setExternalAttributesName($zip_path, ZipArchive::OPSYS_UNIX, $attr); 

    If you are more familiar with Python, see How do I set permissions (attributes) on a file in a ZIP file using Python's zipfile module? 如果您对Python更熟悉,请参阅如何使用Python的zipfile模块在ZIP文件中设置文件的权限(属性)? It deals with permissions only, but you can easily extend it with the symlink bit, as per my PHP example. 它仅处理权限,但您可以使用符号链接位轻松扩展它,根据我的PHP示例。

I'd try keeping the link file inside the gzip (or tar.gz) archive, and only extract it on another linux system. 我会尝试将链接文件保存在gzip(或tar.gz)存档中,并仅在另一个Linux系统上提取它。 I know windows doesn't generally handle linux file attributes & permissions well, and extracting the link on windows probably changes it somehow. 我知道Windows通常不能很好地处理linux文件属性和权限,并且在Windows上提取链接可能会以某种方式改变它。

Or it should be easy to recreate the symlink on the new linux system, either in an automated script or just copy & paste your custom ln line into a terminal, like 或者,应该很容易在自动脚本中重新创建新Linux系统上的符号链接,或者只是将自定义ln行复制并粘贴到终端中,例如

#!/bin/bash
ln -s TARGET LINKNAME

These are all assuming your new linux system has the same target file in the same place as the original linux system. 这些都假设你的新linux系统与原始linux系统在同一个地方有相同的目标文件。

1. With VirtualBox 1.使用VirtualBox

You can use VirtualBox and rsync or extract a tar archive into a Windows host (NTFS) shared folder. 您可以使用VirtualBox和rsync或将tar存档提取到Windows主机(NTFS)共享文件夹中。 However, you need to activate the creation of symbolic links first via 但是,您需要首先激活符号链接的创建

VBoxManage setextradata VM_NAME VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME 1

and your account may not be an administrator account in order to have permissions to create links. 并且您的帐户可能不是管理员帐户,以便拥有创建链接的权限。

2. With Windows Subsystem for Linux (WSL) 2.使用Windows的Linux子系统(WSL)

Alternatively, you can use the Windows Subsystem for Linux (WSL) to extract your tar archive to a NTFS partition. 或者,您可以使用Windows子系统Linux(WSL)将tar存档解压缩到NTFS分区。 It will preserve symbolic links. 它将保留符号链接。 You can also tar these links again and copy the tar archive to a system you like. 您还可以再次tar这些链接并将tar存档复制到您喜欢的系统。

However, so far the WSL is only available on 64-bit editions of Windows 10 and can be activated on Windows 10 Anniversary Update and later. 但是,到目前为止,WSL仅在64位版本的Windows 10上可用,并且可以在Windows 10周年更新及更高版本上激活。 Also note that currently (2018-01-21) the date of files is not restored correctly . 另请注意,当前(2018-01-21) 文件的日期未正确恢复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM