find コマンドでファイルを検索
1 2 | cd /etc/ sudo find . - type f | head -n 4 |
を実行すると、結果は
1 2 3 4 | . /printcap . /networks . /hosts . /securetty |
となる
head コマンドは
ファイルの先頭部分を表示するコマンド
を参考に
-nオプションをつければ出力する数を制限できる
今回なら -n 4なので先頭4行
つまり、 find コマンドの結果から
先頭4行表示している
find コマンドについては
【 find 】 ファイルやディレクトリを検索する
に解説が載っているのでこちらも参考に
今回の
1 | find . - type f |
は現在のディレクトリからファイルを出力
という意味
. は現在のディレクトリという意味
-type f
はファイルのみ出力
という判定にしている
これを利用して特定の拡張子のファイルを探すこともできる
例えば設定ファイルの
.conf 拡張子ファイルを探すのなら
find の結果をパイプで grep に渡すことでできる
ちなみにそのままだとおおいので
head コマンドで10個まで表示にしている
1 | sudo find /etc/ - type f | grep '\.conf$' | head |
結果は
1 2 3 4 5 6 7 8 9 10 | /etc/sysctl .conf /etc/libaudit .conf /etc/depmod .d /dist .conf /etc/ld .so.conf /etc/lvm/lvm .conf /etc/modprobe .d /dist-alsa .conf /etc/modprobe .d /blacklist .conf /etc/modprobe .d /anaconda .conf /etc/modprobe .d /dist-oss .conf /etc/modprobe .d /dist .conf |
$は正規表現で行末という意味
\.としているのは . だと正規表現扱いになるため
今回の conf の部分を php とかに変えれば
特定のソースコードを探すときに使える
なので、もしPHP ファイルを探すなら
| grep ‘\.php$’
となっていた