Symlink
ln:創建相對符號連結到相對符號連結
我想創建一個指向相對符號連結的相對符號連結,而不是相對符號連結的目標。似乎在創建相對符號連結時會
ln
解析TARGET
而不是指向實際的TARGET
. 這個過程可以跳過嗎?在下面的範例中,我想rsym-to-existing-rsym.txt
指向rsym-to-target.txt
,而不是target.txt
。當我為尚不存在的符號連結創建符號連結時,它可以工作(下面範例中的
rsym-to-future-rsym.txt
->future-rsym-to-target.txt
->target.txt
鏈)。例子
# Create initial structure $ mkdir -p temp $ touch temp/target.txt # Now, create relative symlinks $ ln -s -r temp/target.txt temp/rsym-to-target.txt $ ln -s -r temp/rsym-to-target.txt temp/rsym-to-existing-rsym.txt # Note that at this point temp/future-rsym-to-target.txt doesn't exist yet $ ln -s -r temp/future-rsym-to-target.txt temp/rsym-to-future-rsym.txt $ ln -s -r temp/target.txt temp/future-rsym-to-target.txt $ tree -F temp/ # Note that rsym-to-existing-rsym.txt points to target.txt, not to rsym-to-target.txt temp/ ├── future-rsym-to-target.txt -> target.txt ├── rsym-to-existing-rsym.txt -> target.txt ├── rsym-to-future-rsym.txt -> future-rsym-to-target.txt ├── rsym-to-target.txt -> target.txt └── target.txt # Maybe the tools I'm using are just nice to me and resolve final paths? # No, when I delete rsym-to-target.txt symlink rsym-to-existing-rsym.txt still points to target.txt $ rm temp/rsym-to-target.txt tree -F temp/ temp/ ├── future-rsym-to-target.txt -> target.txt ├── rsym-to-existing-rsym.txt -> target.txt ├── rsym-to-future-rsym.txt -> future-rsym-to-target.txt └── target.txt
平原
ln -s
將其目標視為字元串。它不關心該字元串是否恰好是現有文件的路徑。GNU 擴展
ln -sr
將其目標視為文件系統位置。它需要取消引用目標中的目錄,否則生成的實際目標將是錯誤的:mkdir /tmp/demo /tmp/foreign cd /tmp/demo mkdir child ln -s -r foo child/foo # -> ../foo ln -s /tmp/foreign elsewhere ln -s -r foo elsewhere/foo # -> ../demo/foo — not ../foo
這可以在不取消引用路徑的最終組件的情況下實現,但事實並非如此。
該手冊解釋瞭如何獲取與給定目錄相關的連結。而不是使用
ln -r
, callrealpath
(這也是 GNU coreutils 的一部分)來構造相對路徑。呼叫會有所不同,具體取決於您傳遞的是連結名稱還是將在其中創建連結的目錄名稱。如果
$dir
是目錄:ln -s -- "$(realpath -m --relative-to "$dir" -- "$target")" "$dir"
如果
$linkname
是要創建的連結的路徑:ln -s -- "$(realpath -m --relative-to "$(dirname -- "$linkname")" -- "$target")" "$linkname"