LoginSignup
11
8

More than 5 years have passed since last update.

MSYS2でディレクトリのシンボリックリンクを作成する方法

Posted at

Windowsでは、一般ユーザでシンボリックリンクの作成はできません。

$ ln -s /c/Users/kunst/Documents
ln: シンボリックリンク `./Documents' の作成に失敗しました: Permission denied

ディレクトリのリンクを作成したい場合は、cmd.exe組み込みの「mklink」コマンドを使用して、ジャンクションとして作成しましょう!

mklinkコマンドについて

コマンドプロンプト(cmd.exe)から、ヘルプを参照してみましょう。

C:\usr\home\test>mklink /?
シンボリック リンクを作成します。

MKLINK [[/D] | [/H] | [/J]] リンク ターゲット

        /D          ディレクトリのシンボリック リンクを作成します。既定では、
                    ファイルのシンボリック リンクが作成されます。
        /H          シンボリック リンクではなく、ハード リンクを作成します。
        /J          ディレクトリ ジャンクションを作成します。
        リンク      新しいシンボリック リンク名を指定します。
        ターゲット  新しいリンクが参照するパス (相対または絶対)
                    を指定します。

※引数のsourceとdestの順番がlnコマンドと逆なので注意しましょう

mklinkコマンドを使ってジャンクションを作成する

コマンドプロンプト(cmd.exe)から作成する場合

C:\usr\home\test>mklink /j Documents C:\Users\kunst\Documents
Documents <<===>> C:\Users\kunst\Documents のジャンクションが作成されました

C:\usr\home\test>dir
 ドライブ C のボリューム ラベルは Windows です
 ボリューム シリアル番号は 66AD-860B です

 C:\usr\home\test のディレクトリ

2015/10/14  12:44    <DIR>          .
2015/10/14  12:44    <DIR>          ..
2015/10/14  12:44    <JUNCTION>     Documents [C:\Users\kunst\Documents]
               0 個のファイル                   0 バイト
               3 個のディレクトリ  161,501,753,344 バイトの空き領域

msys2のbashから作成する場合

cmd.exeを起動して、実行するコマンドを引数に渡します。
パスの書き方がWindowsの書き方になるので注意

$ cmd.exe /c "mklink /j Documents C:\Users\kunst\Documents"
Documents <<===>> C:\Users\kunst\Documents ▒̃W▒▒▒▒▒N▒V▒▒▒▒▒▒▒쐬▒▒▒▒܂▒▒▒

$ ls -l
合計 0
lrwxrwxrwx 1 kunst なし 24 10月 14 12:45 Documents -> /c/Users/kunst/Documents/

ジャンクションの消し方

消すときはunlinkコマンドで問題ありません。

$ unlink Documents

$ ls
11
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8