第87回 なかなか理解できないfindコマンドのmanを理解してみた
findコマンドが覚えられない
findコマンドというと、
find / -name hoge
で、
ルートディレクトリから再帰的にhogeという名前のファイルもしくはディレクトリを見つけて
という意味になるのだが、この書式をいっこうに覚えられない。
「えーと、検索を開始するディレクトリはどこに書けばよかったけかぁ...」
となる。
そこで、
「manを読めばいいじゃん」
となるのだが、このmanがむずかしい。
これでは、いけないと思い manを理解するように努めてみた。
これは、
findコマンドを理解してみようと試みた私の試練の軌跡である。
findコマンドのmanを読んでみる
findコマンドのmanを見てみる。
今回、重要なところ以外は省略する。
(ちなみに簡単なmanの読み方は ビンゴ中西のほげほげmanのSYNOPSIS(書式)の読み方を参照)
NAME find -- walk a file hierarchy SYNOPSIS find [-H | -L | -P] [-EXdsx] [-f pathname] pathname ... expression find [-H | -L | -P] [-EXdsx] -f pathname [pathname ...] expression DESCRIPTION The find utility recursively descends the directory tree for each pathname listed, evaluating an expression (composed of the ``primaries'' and ``operands'' listed below) in terms of each file in the tree. The options are as follows: -E Interpret regular expressions followed by -regex and -iregex options as extended (modern) regular expressions rather than basic regular expressions (BRE's). The re_format(7) manual page fully describes both formats. ~ 略 ~ PRIMARIES -Bmin n True if the difference between the time of a file's inode cre- ation and the time find was started, rounded up to the next full minute, is n minutes. ~ 略 ~
SYNOPSIS
「検索を開始するディレクトリをどこに書いていいかわからない」のであるから、見るのは、SYNOPSISであろう。ここには書式がかかれているのだ。
SYNOPSIS find [-H | -L | -P] [-EXdsx] [-f pathname] pathname ... expression find [-H | -L | -P] [-EXdsx] -f pathname [pathname ...] expression
しかし、目を凝らしてよく見ても、pathnameを記述できるところが2つもあるではないか。いったいどちらなのだろうか。あぁ、わからない。
そこで、もう一度、
find / -name hoge
この例を思い出すと、検索を開始するpathnameにはオプションがついていないことがわかるので、-fがついていない方のpathnameとわかる。
知ってるオプションがSYNOPSISに載ってない
では、-nameとファイル名の条件を書く部分はどこに書けばいいのかと思ってSYNOPSISを見てみると、-nameが見当たらない。
SYNOPSISからは、
H L P E X d s x f
の9個のオプションしか見受けられない。これは困った。-nameって何やねん。
-nameは何なのか?
SYNOPISISをよく見てみると、expressionという箇所がある。
検索開始ディレクトリを書いたあとには、expressionに該当するものを書かなければいけないようだ。
expressionって何なの?
SYNOPISISだけでは理解できなさそうなので、
DESCRIPTIONを読んでみることにする。
DESCRIPTION The find utility recursively descends the directory tree for each pathname listed, evaluating an expression (composed of the ``primaries'' and ``operands'' listed below) in terms of each file in the tree.
なにやら、expressionは primariesとoperandsからなるらしい。
primariesがなにかわからんが、operandsをとることから、オプションのようなものであると考えることができる。
ここで、ピンときた。-nameは、このprimariesと呼ばれるものではなかろうか!!?
オプションの説明を見てみる
ふとDESCRIPTIONの下に目をやると、
The options are as follows: -E Interpret regular expressions followed by -regex and -iregex options as extended (modern) regular expressions rather than basic regular expressions (BRE's). The re_format(7) manual page fully describes both formats.
と、オプションの説明が始まっている。
おお たしかに、EはSYNPISISにあったオプションである。
この調子で、primariesの説明があって欲しいところ。
primariesの説明はどこか?
期待を胸に、オプションの説明をずーと下にスクロールしていくと今度は、
PRIMARIES -Bmin n True if the difference between the time of a file's inode cre- ation and the time find was started, rounded up to the next full minute, is n minutes.
と、primarisの説明が始まったではないか!!!
さらに下にスクロールすると、確かにprimarisの説明として、-nameの説明があった。
-name pattern True if the last component of the pathname being examined matches pattern. Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'') may be used as part of pattern. These characters may be matched explicitly by escaping them with a backslash (``\'').
なぜfindがわからなかったのか
なぜfindコマンドがむずかしかったのだろうか?
以下の点が上げられるように思える。
- expressionがprimarisとoperandsから成るということを理解していなかった(primarisの概念がなかった)
- 検索を開始するディレクトリをどこに書けばいいのかわかってなかった(-fオプションに注目できていればすぐに気づけた)
- SYNOPSISに知っているオプションがなくてわけがわからなくなっていた(オプションではなくprimarisだったのだ)