そこで、シンボリックリンクを作成しました。
ln -s /location/to/link linkname
次に、シンボリックリンクがリンクする場所を変更したいと思います。それ、どうやったら出来るの?最初に削除せずにそれを行う方法はありますか?
別の名前で新しいリンクを作成し、それを移動して古いリンクを置き換えることができます。
ln -s /location/to/link linkname
後で
ln -s /location/to/link2 newlink
mv newlink linkname
newlink
とlinkname
が同じ物理デバイス上にある場合はmv
、アトミックである必要があります。
試してみてくださいln -sf new_destination linkname
。
シンボリックリンクのターゲットを変更するだけです:
# ln -sfT /path/to/new/target linkname
これは、瞬時のアトミックな変更です。
-T
シンボリック リンクのターゲットがディレクトリの場合は、フラグをコマンドに追加する必要がありますmv
。そうしないと、新しいシンボリック リンクが古いシンボリック リンクのターゲット ディレクトリに移動します。
Web サイトを新しいバージョンにアトミックに切り替える例:
元のセットアップ - Web サイトはwww1
ディレクトリに保存され、vhost はwww
シンボリック リンクを指します。
ln -s www1 www
ウェブサイトを参照して、古いバージョンを参照してください。
新しい Web サイト ファイルを新しいwww2
ディレクトリに配置します。
新しい Web サイトへの新しいシンボリック リンクを設定します。
ln -s www_new www2
www
シンボリック リンクを新しい Web サイトのディレクトリに移動します。
mv -T www_new www
ウェブサイトを参照して、新しいバージョンをすぐに確認してください。
OSXでは、lnのmanページには、このようにできると書かれています
ln -shf /location/to/link link name
マニュアルページから:
The options are as follows:
-F If the target file already exists and is a directory, then remove it so that the link may occur. The -F
option should be used with either -f or -i options. If none is specified, -f is implied. The -F option is
a no-op unless -s option is specified.
-h If the target_file or target_dir is a symbolic link, do not follow it. This is most useful with the -f
option, to replace a symlink which may point to a directory.
-f If the target file already exists, then unlink it so that the link may occur. (The -f option overrides any
previous -i options.)
-i Cause ln to write a prompt to standard error if the target file exists. If the response from the standard
input begins with the character `y' or `Y', then unlink the target file so that the link may occur. Other-
wise, do not attempt the link. (The -i option overrides any previous -f options.)
-n Same as -h, for compatibility with other ln implementations.
-s Create a symbolic link.
-v Cause ln to be verbose, showing files as they are processed.
いいえ。newpathがすでに存在する場合、symlink
システムコールが返されます。EEXIST
ファイルシステム内の新しいノードからのみリンクできます。ここでの要件は何ですか?unlink / symlink呼び出しの非アトミック性が原因で競合が心配な場合は、アーキテクチャを少し考え直して、他の場所で同期を提供することをお勧めします。この種のものによって導入されたいくつかの恐ろしいセキュリティバグがありました。
次のようにコマンドをチェーンします。
rm currentlink && ln -s /path/to/link currentlink
最初のコマンドは既存のものを削除し、2 番目のコマンドはすぐに再作成します。
他の人が述べたように、基本的には、手動で、または-f
フラグをln
ユーティリティに渡して、最初にシンボリックリンクを削除する必要があります。
edln
何年も前に、私はかなり頻繁にシンボリック リンクに小さな編集を加えなければならなかったので、この煩わしさを軽減するために単純な readline ベースのユーティリティ ( ) を書きました。他の誰かが役に立つと思った場合に備えて、https://github.com/jjlin/edln/でオンラインにしました。
edln
元のシンボリック リンク ターゲットが表示されます。その後、矢印キーまたは標準の readline キーストローク ( M-b
、M-f
、C-d
など) を使用して、ターゲットを移動および編集できます。
グーグルで調べただけで、良い答えが見つからず、自分で解決する必要がありました:
ln -f -s -T `readlink SomeLibrary | sed 's/version.old/version.new/'` SomeLibrary
定義による編集とは、ゼロから作り直すのではなく、部分的に変更することを意味します。長いパスや奇妙な記号を含むパスを記憶する必要がある回答は、間違いなく悪いものです。