findコマンド
以前の記事Linuxのfindコマンド使用方法の備忘
で、findコマンドの使用方法を少し記載しましたが、先日仕事で別の使い方して「これってめちゃくちゃ便利!」ってなったのでこちらに記載したいと思います。
環境
本記事で記載しているコマンドは以下環境下で実施しています。
ローカル:maxOS High Sierra バージョン 10.13.4
リモート:CentOS release 6.9 (Final)
findコマンドの判別式type
について
findコマンドでは判別式にtype
といった式があり、検索条件にファイルやディレクトリなどを指定することが出来ます。簡単にどういうことかコマンドで以下に記載します。
・カレントディレクトリを確認
[shota@hostname work]$ ls -l
合計 12
drwxrwxr-x 2 shota shota 4096 8月 8 21:44 2018 dir1
drwxrwxr-x 2 shota shota 4096 8月 8 21:44 2018 dir2
drwxrwxr-x 2 shota shota 4096 8月 8 21:44 2018 dir3
-rw-rw-r-- 1 shota shota 0 8月 8 21:45 2018 file1
-rw-rw-r-- 1 shota shota 0 8月 8 21:45 2018 file2
-rw-rw-r-- 1 shota shota 0 8月 8 21:45 2018 file3
・判別式type
で通常ファイルを検索
判別式type
の書式は以下の通りでc
に検索する種別を指定します。
書式:-type c
以下の通り、-type f
とすると通常ファイルのみ検索されます。
[shota@hostname work]$ find . -type f
./file3
./file1
./file2
・判別式type
でディレクトリを検索
今度はディレクトリのみ検索といったケースです。
以下の通り、-type d
とするとディレクトリのみ検索されます。
[shota@hostname work]$ find . -type d
.
./dir1
./dir2
./dir3
検索できる種別はもちろん、f
やd
以外にも色々とありますのでぜひ使ってみてください。
※man find
コマンドで使用可能な種別が確認できます。
以上