LoginSignup
83
79

More than 5 years have passed since last update.

リンク切れのシンボリックリンクを探す方法

Last updated at Posted at 2013-04-04

結論

を先に書くと

リンク切れのシンボリックリンクは find -xtype l で探せる

よ!

manによると

-xtype c
ファイルがシンボリックリンク以外の場合は -type と同じ。シンボリックリンクに対しては以下のように判断される。この判別式の前に -follow が指定されていなかった場合は、リンク先のファイルのタイプが c ならば真。 -follow が指定されていた場合は c が ‘l’ ならば真。つまりシンボリックリンクに対して、 -xtype は -type がチェックしない側をチェックする。

となっており、うーん分かりにくい…。

-typeと-xtypeの違いを確認

まず試験用のファイルを作って…

$ touch file
$ ln -s file link-file
$ ln -s link-file link-link-file
$ ln -sf null link-null
$ mkdir dir
$ find -ls
113575 4 drwxr-xr-x 2 root root 4096  4月  4 13:19 .
113698 4 drwxr-xr-x 1 root root 4096  4月  4 13:19 ./dir
113638 0 -rw-r--r-- 1 root root    0  4月  4 13:17 ./file
113984 0 lrwxrwxrwx 1 root root    4  4月  4 13:17 ./link-file -> file
113985 0 lrwxrwxrwx 1 root root    9  4月  4 13:18 ./link-link-file -> link-file
113593 0 lrwxrwxrwx 1 root root    4  4月  4 13:19 ./link-null -> null

で、-type-xtype、それから-followをつけると動作が変わるらしいのでそれらを比較。

$ find -type f
./file

$ find -type l
./link-file
./link-link-file
./link-null

$ find -xtype f
./file
./link-file
./link-link-file

$ find -xtype l
./link-null

$ find -follow -xtype f
./file

$ find -follow -xtype l
./link-file
./link-link-file
./link-null

$ find -empty
./dir
./file

なるほど。ついでに-emptyの動作も再確認してみたがこれは-typefdのときしか反応しない、と。

比較しやすいようテーブルにしてみる

dir file link-file link-link-file link-null
-type f
-type l
-xtype f
-xtype l
-follow -xtype f
-follow -xtype l
-empty

-followが付いてると-xtype-typeと同じ動作になるっぽい、まぁ意味的に当然そうなるわな。-follow自体は-xtypeだけで使うオプションではないのでその事自体は別によい。

注意な

わざわざ-followについて言及するのは、大事なのは、

-followが付いてる時、-xtypeはあなたが期待するのと違う動作になってる可能性が高いから注意しろよ

ってことだと思う。

83
79
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
83
79