1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

findの-typeオプションによるファイルやディレクトリの一覧表示

Last updated at Posted at 2018-08-08

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

検索できる種別はもちろん、fd以外にも色々とありますのでぜひ使ってみてください。
man findコマンドで使用可能な種別が確認できます。

以上

1
2
3

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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?