RAKUS Developers Blog | ラクス エンジニアブログ

株式会社ラクスのITエンジニアによる技術ブログです。

find コマンド 【使い方 まとめ】

技術広報のyayawowoです。

今回は、Linux上でファイルやディレクトリ検索時に利用する
『find コマンド』について紹介します!
find コマンドの基本をはじめ、活用例をコマンドサンプルを交えて説明させていただきます。

【目次】

Linuxの理解をより深めたい方へ以下関連おすすめブログ
ls コマンド 【使い方 まとめ】
よく使うLinuxコマンド一覧【最新版】
sar コマンド【使い方 まとめ】
iptables まとめ【Linux ファイアウォール】
sed コマンド【使い方 まとめ】
vi コマンド【使い方まとめ】
Linuxのファイル操作でよく使うLinuxコマンド
初心者のためのawkコマンド
実務で使える!基本的なシェル(Linux)コマンドの話 ~forとsed~
【Linux】今振り返りたい、プロセスって何?

find コマンドとは

find コマンドは、検索するためのコマンドです。
ファイルやディレクトリを検索する際に用います。

find コマンドの使い方

基本書式

find コマンドの基本書式は以下の通りです。

$ find 検索場所 [オプション] ファイル名

ファイルを検索するには、そのファイルがどこにあるのかを指定する必要があります。
また、find コマンドには多くのオプションがあります。
以下にまとめておきますので、用途に合わせて使い分けください。

オプション一覧

オプション 説明
-name ファイル名を指定検索
-iname 文字の大小を区別せず、ファイル名を指定検索
-type f ファイルのみを対象に検索
-type d ディレクトリを対象に検索
-type p 名前付きパイプを対象に検索
-empty ファイル容量が0のファイル/ディレクトリを対象に検索
-size ファイル容量を指定検索
-path ファイルパスを指定検索
-ipath 文字の大小を区別せず、ファイルパスを指定検索
-newer 指定したファイルの更新日時後に更新された、ファイル/ディレクトリを検索
-anewer 指定したファイルの更新日時後にアクセスされた、ファイル/ディレクトリを検索
-mtime 日数 最後にデータが更新された日時を検索
-atime 日数 指定日数以前にアクセスされた、ファイル/ディレクトリを対象に検索
-mmin 時間 指定分数に更新された、ファイル/ディレクトリを対象に検索
-amin 指定分数にアクセスされた、ファイル/ディレクトリを対象に検索

演算子/アクション一覧

演算子/アクション 説明
-a 複数条件検索
-and 複数条件(AND検索)
-or 複数条件(OR検索)
-not 条件不一致検索
-exec 検索結果に対してコマンドを実行

find コマンドの活用例

  • 指定したファイル名を検索する(-name)
# ファイル/ディレクトリ一覧を表示
localhost:/home/dir1# ls -1
dir1_1
dir1_2
sample.txt
sample2.txt
sample3.txt

# 「sample2.txt」を指定検索
# findコマンドと-name
localhost:/home/dir1# find /home/dir1/ -name sample2.txt
/home/dir1/sample2.txt
  • ファイルのみを対象に検索(-type f)
# ファイル/ディレクトリ一覧を表示
localhost:/home/dir1# ls -1
dir1_1
dir1_2
sample.txt
sample2.txt
sample3.txt

# ファイルのみを対象に検索
# findコマンドと-type f
localhost:/home/dir1# find /home/dir1/ -type f
/home/dir1/sample.txt
/home/dir1/sample2.txt
/home/dir1/sample3.txt
# ファイル/ディレクトリ一覧を表示
localhost:/home/dir1# ls -1
dir1_1
dir1_2
sample.txt
sample2.txt
sample3.txt

# ディレクトリのみを対象に検索
# findコマンドと-type d
localhost:/home/dir1# find /home/dir1/ -type d
/home/dir1/
/home/dir1/dir1_1
/home/dir1/dir1_2
  • ファイル容量を指定検索(-size)
# ファイル/ディレクトリ一覧を表示
localhost:/home/dir1# ls -la
total 20
drwxr-xr-x    4 root     root           166 Aug 26 16:47 .
drwxr-xr-x    5 root     root           100 Jul  5  2020 ..
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_1
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_2
-rw-r--r--    1 root     root            13 Aug 26 16:48 sample.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample2.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample3.txt

# ファイル容量が10byte以下のファイルを検索
# findコマンドと-size
localhost:/home/dir1# find /home/dir1/ -size -10c
/home/dir1/sample2.txt
/home/dir1/sample3.txt
  • 指定したファイルの更新日時後に更新された、ファイル/ディレクトリを検索(-newer)

※2022/8/26を今日(当日)とします。

# ファイル/ディレクトリ一覧を表示
localhost:/home/dir1# ls -la
total 20
drwxr-xr-x    4 root     root           166 Aug 26 16:47 .
drwxr-xr-x    5 root     root           100 Jul  5  2020 ..
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_1
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_2
-rw-r--r--    1 root     root            13 Aug 26 16:48 sample.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample2.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample3.txt

# 「sample.txt」より後に更新したファイル/ディレクトリを検索
# findコマンドと-newer
localhost:/home/dir1# find /home/dir1/ -newer sample.txt
/home/dir1/dir1_1
/home/dir1/dir1_2
  • 最後にデータが更新された日時を検索(-mtime 日数)

※日数の定義
 今日:0
 昨日:1
※2022/8/26を今日(当日)とします。

# ファイル/ディレクトリ一覧を表示
localhost:/home/dir1# ls -la
total 20
drwxr-xr-x    4 root     root           166 Aug 26 16:47 .
drwxr-xr-x    5 root     root           100 Jul  5  2020 ..
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_1
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_2
-rw-r--r--    1 root     root            13 Aug 26 16:48 sample.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample2.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample3.txt

# 今日更新されたファイル/ディレクトリを検索
localhost:/home/dir1# find /home/dir1/ -mtime 0
/home/dir1/
/home/dir1/sample.txt
/home/dir1/sample2.txt
/home/dir1/sample3.txt
/home/dir1/dir1_1
/home/dir1/dir1_2

# 最後にデータが更新されたファイル/ディレクトリを検索
# findコマンドと-mtime
localhost:/home/dir1# find /home/dir/ -mtime 1
find: /home/dir/: No such file or directory
  • 指定分数に更新された、ファイル/ディレクトリを対象に検索(-mmin 時間)

※時間の定義
 1分:1
 6時間:60分×6時間=360
 12時間:60分×12時間=720
※2022/8/26を今日(当日)とします。

# ファイル/ディレクトリ一覧を表示
localhost:/home/dir1# ls -la
total 20
drwxr-xr-x    4 root     root           166 Aug 26 16:47 .
drwxr-xr-x    5 root     root           100 Jul  5  2020 ..
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_1
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_2
-rw-r--r--    1 root     root            13 Aug 26 16:48 sample.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample2.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample3.txt

# 30分以内に更新されたファイル/ディレクトリを検索
# findコマンドと-mmin
localhost:/home/dir1# find /home/dir1/ -mmin -30
/home/dir1/
/home/dir1/sample.txt
/home/dir1/sample2.txt
/home/dir1/sample3.txt
/home/dir1/dir1_1
/home/dir1/dir1_2
  • 複数条件(AND検索)(-and)

-type f-mtime 日数を組み合わせて検索します。

※2022/8/26を今日(当日)とします。

# ファイル/ディレクトリ一覧を表示
localhost:/home/dir1# ls -la
total 20
drwxr-xr-x    4 root     root           166 Aug 26 16:47 .
drwxr-xr-x    5 root     root           100 Jul  5  2020 ..
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_1
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_2
-rw-r--r--    1 root     root            13 Aug 26 16:48 sample.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample2.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample3.txt

# 今日更新されたファイルのみを検索
# findコマンドと-and
localhost:/home/dir1# find /home/dir1/ -type f -and -mtime 0
/home/dir1/sample.txt
/home/dir1/sample2.txt
/home/dir1/sample3.txt
  • 複数条件(OR検索)(-or)

-sizeを組み合わせて検索します。

# ファイル/ディレクトリ一覧を表示
localhost:/home/dir1# ls -la
total 20
drwxr-xr-x    4 root     root           166 Aug 26 16:47 .
drwxr-xr-x    5 root     root           100 Jul  5  2020 ..
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_1
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_2
-rw-r--r--    1 root     root            13 Aug 26 16:48 sample.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample2.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample3.txt

# ファイル容量が1byte以下又は、15byte以下のファイルを検索
# findコマンドと-or
localhost:/home/dir1# find /home/dir1/ -size -1c -or -size -15c
/home/dir1/sample.txt
/home/dir1/sample2.txt
/home/dir1/sample3.txt
  • 条件不一致検索(-not)

-type f-notを組み合わせて、ファイル以外を検索します。

# ファイル/ディレクトリ一覧を表示
localhost:/home/dir1# ls -la
total 20
drwxr-xr-x    4 root     root           166 Aug 26 16:47 .
drwxr-xr-x    5 root     root           100 Jul  5  2020 ..
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_1
drwxr-xr-x    2 root     root            37 Aug 26 16:49 dir1_2
-rw-r--r--    1 root     root            13 Aug 26 16:48 sample.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample2.txt
-rw-r--r--    1 root     root             0 Aug 26 16:48 sample3.txt

# ファイル以外を検索
# findコマンドと-not
localhost:/home/dir1# find /home/dir1/ -not -type f
/home/dir1/
/home/dir1/dir1_1
/home/dir1/dir1_2

終わりに

いかがでしたでしょうか?
今回はファイルやディレクトリ検索にて良く用いる、find コマンドの使い方をご紹介させていただきました。
find コマンドはオプションや、演算子等を使い分けることで様々な検索方法ができます。
Linuxコマンドをまとめたブログもございますので、是非ご参考ください。

最後までお読みいただきありがとうございました!


◆TECH PLAY
techplay.jp

◆connpass
rakus.connpass.com

Copyright © RAKUS Co., Ltd. All rights reserved.