シンボリックリンクということは、オリジナルのファイルやフォルダといったものが存在することになる。 そして、シンボリックリンクはファイルやフォルダに対してのみではなく、シンボリックリンクに対しても作成することが出来る。
今回は、このシンボリックリンクとオリジナルファイルの関連を追う方法について紹介する。 なお、サンプル環境は以下のようになっている。
shell[root@test-centos7 ~]# ls -la $(find /sample* -type l -or -type f) -rw-r--r--. 1 root root 0 6月 6 17:18 /sample/test.txt lrwxrwxrwx. 1 root root 16 6月 6 17:19 /sample2/test.txt -> /sample/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample3/test.txt -> /sample2/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample4/test.txt -> /sample3/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample5/test.txt -> /sample4/test.txt
readlinkを用いる
readlinkを用いる事で、シンボリックリンクからオリジナルファイルの場所を調べる事が出来る。
bashreadlink シンボリックリンクのPATH
shell[root@test-centos7 ~]# readlink /sample5/test.txt /sample4/test.txt
しかし、オプションなしだとシンボリックリンクの直接のリンク先までしか表示されない。 オリジナルファイルのPATHを表示させるならば、「-f」オプションを利用する。
bashreadlink -f シンボリックリンクのPATH
shell[root@test-centos7 ~]# readlink -f /sample5/test.txt /sample/test.txt
これでオリジナルファイルのPATHが確認出来る。 しかし、間に挟まっているシンボリックリンクについての情報を出力させることが出来ないので、その辺りの情報が必要な場合、後述する2つの方法のどちらかを採用すると良いだろう。
nameiコマンドでツリー状に表示させる
nameiコマンドで、階層状にシンボリックリンクをたどっていき、オリジナルファイルのPATHにたどり着く事が出来る。
bashnamei シンボリックリンクのPATH
shell[root@test-centos7 ~]# namei /sample5/test.txt f: /sample5/test.txt d / d sample5 l test.txt -> /sample4/test.txt d / d sample4 l test.txt -> /sample3/test.txt d / d sample3 l test.txt -> /sample2/test.txt d / d sample2 l test.txt -> /sample/test.txt d / d sample - test.txt
より細かい情報(パーミッションやオーナー、グループなど)を取得する場合は、「-l」オプションを付与する。
bashnamei -l シンボリックリンクのPATH
shell[root@test-centos7 ~]# namei -l /sample5/test.txt f: /sample5/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample5 lrwxrwxrwx root root test.txt -> /sample4/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample4 lrwxrwxrwx root root test.txt -> /sample3/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample3 lrwxrwxrwx root root test.txt -> /sample2/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample2 lrwxrwxrwx root root test.txt -> /sample/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample -rw-r--r-- root root test.txt
なお、シンボリックリンクをたどってオリジナルファイルにたどり着く事はできるのだが、自身に対しシンボリックリンクが作成されているかどうかは調べる事が出来ない。 例えば「/sample4/test.txt」で調べると、オリジナルファイルへたどり着くまでのPATHは表示されるのだが、自分へのシンボリックリンクである「/sample5/test.txt」は表示されない。
shell[root@test-centos7 ~]# namei -l /sample5/test.txt f: /sample5/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample5 lrwxrwxrwx root root test.txt -> /sample4/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample4 lrwxrwxrwx root root test.txt -> /sample3/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample3 lrwxrwxrwx root root test.txt -> /sample2/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample2 lrwxrwxrwx root root test.txt -> /sample/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample -rw-r--r-- root root test.txt [root@test-centos7 ~]# namei -l /sample4/test.txt f: /sample4/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample4 lrwxrwxrwx root root test.txt -> /sample3/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample3 lrwxrwxrwx root root test.txt -> /sample2/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample2 lrwxrwxrwx root root test.txt -> /sample/test.txt drwxr-xr-x root root / drwxr-xr-x root root sample -rw-r--r-- root root test.txt
それを調べる場合は、後述するfindコマンドで調べる方法を利用してもらいたい。
findコマンドで全てのシンボリックリンクとオリジナルファイルを表示させる
以前にオリジナルファイルからシンボリックリンクを逆にたどる方法でも記述したが、findコマンドを用いる事で全てのシンボリックリンクとそのオリジナルファイルの情報を取得することが出来る。
bashls -la $(find -L /検索対象とするディレクトリ -samefile シンボリックリンクのPATH)
shell[root@test-centos7 ~]# ls -la $(find -L /{sample*,work,home} -samefile /sample5/test.txt) -rw-r--r--. 1 root root 0 6月 6 17:18 /sample/test.txt lrwxrwxrwx. 1 root root 16 6月 6 17:19 /sample2/test.txt -> /sample/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample3/test.txt -> /sample2/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample4/test.txt -> /sample3/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample5/test.txt -> /sample4/test.txt
この方法であれば、自分自身へ作成されたシンボリックリンクの存在についても認識することが出来る。
shell[root@test-centos7 ~]# ls -la $(find -L /{sample*,work,home} -samefile /sample4/test.txt) -rw-r--r--. 1 root root 0 6月 6 17:18 /sample/test.txt lrwxrwxrwx. 1 root root 16 6月 6 17:19 /sample2/test.txt -> /sample/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample3/test.txt -> /sample2/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample4/test.txt -> /sample3/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample5/test.txt -> /sample4/test.txt [root@test-centos7 ~]# ls -la $(find -L /{sample*,work,home} -samefile /sample3/test.txt) -rw-r--r--. 1 root root 0 6月 6 17:18 /sample/test.txt lrwxrwxrwx. 1 root root 16 6月 6 17:19 /sample2/test.txt -> /sample/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample3/test.txt -> /sample2/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample4/test.txt -> /sample3/test.txt lrwxrwxrwx. 1 root root 17 6月 6 17:19 /sample5/test.txt -> /sample4/test.txt
ただし、リンクの相互関係は別に順番通りになるわけではなく、基本的には目視での確認になってしまう。 それぞれ一長一短あるので、その状況に合わせて利用してもらいたい。