【问题标题】:link files within directory, with simple command similar to cp链接目录中的文件,使用类似于 cp 的简单命令
【发布时间】:2016-05-11 15:01:34
【问题描述】:

我的问题来自哪里:

  • 运行cp source/files.all destination/ 时,source 中的所有文件现在也将存在于destination

问题:

  • 如果我不想将source 中的数据复制到destination 中,而只是将它们链接起来(使用绝对路径)。通常,我会运行类似:

    for f in $(ls source/); do ln -s $(pwd)/${f} $(pwd)/destination; done

    是否有一个我可以使用的简单命令/工具(例如ln -a source/files.all destination/)可以创建指向目录中所有文件的软链接,同时自动添加绝对路径作为前缀。 ln -r 接近我需要的,但绝对路径,而不是相对路径?

【问题讨论】:

  • 不,循环方法是您可以使用的。在您要创建链接的目录中,您可以执行类似for i in source/*; do; [ -f "$i" ] && ln -s "$i"; done 的操作
  • @DavidC.Rankin : 也许,操作不应该解析 ls 的输出。

标签: linux bash ln


【解决方案1】:

我会使用find "$PWD/source" -exec ln -s {} destination \;。用作find 的第一个参数的绝对路径将导致{} 被每个命令的源文件的绝对路径替换。

GNU ln 支持 -t 选项来指定目标目录,允许您使用更有效的 find 调用:

find "$PWD/source" -exec ln -s -t destination {} +

-exec ... + 形式要求{} 是命令中的最后一个参数; -t 允许您将目标参数向上移动以适应该要求。

【讨论】:

    【解决方案2】:

    所以我最终找到了一个简单的方法来做到这一点:

    只需运行ln -s $(pwd -P)/source/* destination/

    【讨论】:

      猜你喜欢
      • 2013-10-28
      • 2014-05-15
      • 2018-11-18
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2013-12-05
      相关资源
      最近更新 更多