复制包含符号链接的继承目录

时间:2013-05-14 16:37:37

标签: php bash symlink

这类似于之前提出过的问题,但我保证会有所不同。

我有一个涉及从父级到子级站点继承的站点。部分内容是媒体。为了使媒体服务更快,我正在创建子目录中父目录的符号链接副本,然后覆盖那些包含子具体定义的文件的副本。这允许apache从媒体(可能来自父级)静态地提供服务,同时从父级继承html。

继承是常见的构建之后的示例 - > maps-> cars:

/some/directory/common/
   images/
      cats.png
      muffins.png
   css/
      styles.css

/some/directory/maps/
   images/
      cats.png -> /some/directory/common/images/cats.png
      muffins.png (overridden by child)
   css/
      styles.css -> /some/directory/common/css/styles.css
      mapsStyles.css (child only)

/some/directory/cars/
   images/
      cats.png -> /some/directory/common/images/cats.png
      muffins.png -> /some/directory/maps/images/muffins.png
   css/
      styles.css -> /some/directory/common/css/styles.css
      carsStyles.css (child only)

所以我可以符号链接到/images/muffins.png并根据我所在的网站接收正确的媒体。特定于站点的媒体存储在相同的目录结构中,但位于仅包含该站点特定媒体的不同位置。

问题在于,无论我如何尝试,第二个孩子最终都会向其父级而不是发起者添加符号链接。在上面的例子中,我不能让/some/direcory/cars/images/cats.png以编程方式指向公共媒体,只能指向地图媒体。

目前我正在使用命令cp -sR /some/directory/maps/* /some/directory/cars/

我在PHP上运行它,但目前在PHP cp函数中使用lnexec()命令来构建这些结构。

2 个答案:

答案 0 :(得分:1)

您似乎应该使用tar而不是cp: 请尝试(我现在无法测试;请注意默认情况下使用符号链接你的 tar的versino。它应该将它们保存为符号链接,而不是遵循它们......但是ymmv):< / p>

#step 1: create an archive of one of the directories_using_common_symlinks:
cd /some/directory/maps/
tar cvf /somewhere/wholeshebang.tar images/ css/
        #ie, tar everything (directories, symlinks, etc).
        #Please add whatever is missing
        #note: it will also contain extra (specific) stuff, which we get rid of below

#step 2: create the real archive of JUST the common stuff
cd /some/temporary_dir/
tar xvf /somewhere/wholeshebang.tar  #regurgitate all the content.
rm      /somewhere/wholeshebang.tar  #we won't need that one anymore, it contains too much
rm images/muffins.png  css/mapStyles.css #delete all extra stuff
tar cvf /somewhere/just_commons.tar  #recreate tar, this time with only common stuff.

#step 3: use the new archive to "kickstart" the other directories
cd /the/destination
tar xvf /somewhere/just_commons.tar  #regurgitte the common stuff (dirs, symlinks)
#then add whatever is specific to /the/destination

答案 1 :(得分:0)

如果问题只是链接.....你可以尝试&#34; cp -l&#34; ....(查看man cp获取更多信息)

手册页:

-l, --link
     hard link files instead of copying

因此应该解决这个问题!