はじめに

こんにちは!今回はfindコマンドについて解説します。

findは、ファイルやディレクトリを検索するコマンドです。名前、サイズ、更新日時など、様々な条件で検索できます。「あのファイルどこだっけ?」って時に超便利。

findコマンドとは

findは、ディレクトリツリーを再帰的に検索して、指定した条件に合うファイルやディレクトリを見つけるコマンドです。

検索条件が豊富で、見つけたファイルに対してコマンドを実行することもできます。Linuxのファイル管理に欠かせないコマンドです。

基本構文

1
find [検索パス] [検索条件] [アクション]

主な検索条件

オプション 説明
-name パターン ファイル名で検索(大文字小文字を区別)
-iname パターン ファイル名で検索(大文字小文字を区別しない)
-type f ファイルのみ
-type d ディレクトリのみ
-type l シンボリックリンクのみ
-size +10M 10MBより大きい
-size -1k 1KBより小さい
-mtime -7 過去7日以内に変更
-mtime +30 30日以上前に変更
-user ユーザー名 特定のユーザーが所有
-perm 644 パーミッションが644
-empty 空のファイル・ディレクトリ

主なアクション

オプション 説明
-print ファイル名を表示(デフォルト)
-delete 見つけたファイルを削除
-exec コマンド {} \; 見つけた各ファイルでコマンド実行
-ok コマンド {} \; 確認してからコマンド実行
-ls ls -l形式で表示

使用例

例1: 基本的な検索

1
find . -name "*.txt"

実行結果:

1
2
3
./file1.txt
./documents/note.txt
./backup/data.txt

カレントディレクトリ以下の全ての.txtファイルを検索します。

例2: 大文字小文字を区別しない検索

1
find . -iname "readme*"

実行結果:

1
2
3
./README.md
./readme.txt
./ReadMe.doc

-inameで大文字小文字を気にせず検索できます。

例3: ファイルタイプで絞り込み

1
find /var/log -type f -name "*.log"

ファイルのみを検索(ディレクトリは除外)。

1
find . -type d -name "test*"

ディレクトリのみを検索。

例4: サイズで検索

1
find . -size +100M

100MBより大きいファイルを検索。

1
find . -size -1k

1KBより小さいファイルを検索。

1
find /tmp -size +10M -size -100M

10MB以上100MB未満のファイル。

例5: 更新日時で検索

1
find . -mtime -7

過去7日以内に変更されたファイル。

1
find . -mtime +30

30日以上前に変更されたファイル。

1
find . -mmin -60

過去60分以内に変更されたファイル。

例6: 空のファイル・ディレクトリを検索

1
find . -type f -empty

実行結果:

1
2
./empty_file.txt
./temp/placeholder.txt

空のファイルを見つけます。

1
find . -type d -empty

空のディレクトリを見つけます。

例7: パーミッションで検索

1
find . -perm 777

パーミッションが777のファイルを検索(セキュリティチェックに便利)。

1
find . -perm /u+x

実行可能なファイルを検索。

例8: 複数条件の組み合わせ(AND)

1
find . -name "*.log" -size +1M

.logファイルで、かつ1MBより大きいもの。

1
find . -type f -name "*.sh" -perm /u+x

実行可能な.shファイル。

例9: 複数条件の組み合わせ(OR)

1
find . -name "*.txt" -o -name "*.md"

.txtまたは.mdファイルを検索。

例10: 特定のディレクトリを除外

1
find . -name "*.js" -not -path "*/node_modules/*"

node_modulesディレクトリを除外して.jsファイルを検索。

例11: 見つけたファイルに対してコマンド実行

1
find . -name "*.tmp" -exec rm {} \;

見つけた.tmpファイルを全て削除。

1
find . -type f -name "*.txt" -exec wc -l {} \;

見つけた.txtファイルの行数を表示。

1
find . -name "*.jpg" -exec cp {} /backup/ \;

見つけた画像ファイルをバックアップディレクトリにコピー。

例12: 確認してからコマンド実行

1
find . -name "*.bak" -ok rm {} \;

削除前に確認メッセージが表示されます。

例13: 複数ファイルをまとめて処理

1
find . -name "*.log" -exec grep "ERROR" {} +

{} \;ではなく{} +を使うと、複数ファイルをまとめて処理できて高速。

例14: 検索結果の数をカウント

1
find . -type f -name "*.js" | wc -l

JavaScriptファイルの数を数えます。

例15: 所有者で検索

1
find /home -user john

johnというユーザーが所有するファイルを検索。

実践的な使い方

ディスク容量を圧迫している大きいファイルを見つける

1
find / -type f -size +500M -exec ls -lh {} \; 2>/dev/null

500MB以上のファイルをサイズ順に表示。

または

1
find / -type f -size +500M -exec du -h {} \; | sort -rh | head -20

古いログファイルを削除

1
find /var/log -name "*.log" -mtime +90 -delete

90日以上前のログファイルを削除。

最近変更されたファイルを確認

1
find . -type f -mtime -1 -ls

過去24時間以内に変更されたファイルを詳細表示。

実行可能ファイルを見つける

1
find . -type f -perm /u+x

セキュリティチェック(権限が緩いファイル)

1
find / -type f -perm 777 2>/dev/null

誰でも書き込み可能なファイルを検索。

バックアップスクリプト

1
2
#!/bin/bash
find /home/user/documents -type f -mtime -7 -exec cp --parents {} /backup/ \;

過去7日間に変更されたファイルをバックアップ。

空のディレクトリを削除

1
find . -type d -empty -delete

特定の拡張子のファイルをまとめて移動

1
find . -name "*.pdf" -exec mv {} /documents/pdfs/ \;

ファイル名を一括変更

1
find . -name "*.jpeg" -exec rename 's/\.jpeg$/.jpg/' {} \;

重複ファイルを探す(簡易版)

1
find . -type f -exec md5sum {} + | sort | uniq -w32 -dD

プロジェクト内のコード行数をカウント

1
find . -name "*.js" -not -path "*/node_modules/*" -exec wc -l {} + | tail -1

破損したシンボリックリンクを見つける

1
find . -type l ! -exec test -e {} \; -print

パーミッションを一括変更

1
2
3
4
5
# ディレクトリを755に
find . -type d -exec chmod 755 {} \;

# ファイルを644に
find . -type f -exec chmod 644 {} \;

ファイルの一覧をテキストファイルに保存

1
find /project -name "*.py" > python_files.txt

特定期間内のファイルを検索

1
2
3
4
5
# 2024年1月1日以降に変更されたファイル
find . -type f -newermt "2024-01-01"

# 特定の日付範囲
find . -type f -newermt "2024-01-01" ! -newermt "2024-02-01"

Tips・注意点

  • パスは必ず指定: パスを省略すると現在のディレクトリから検索

    1
    2
    
    find . -name "*.txt"  # カレントディレクトリ
    find /home -name "*.txt"  # /homeディレクトリ
    
  • ワイルドカードはクォートで囲む: シェルの展開を防ぐ

    1
    2
    
    find . -name "*.txt"  # 正しい
    find . -name *.txt    # 間違い
    
  • -deleteは慎重に: 削除は取り消せない

    1
    2
    
    find . -name "*.tmp" -print  # まず確認
    find . -name "*.tmp" -delete  # 確認後に実行
    
  • エラーメッセージを抑制: 権限エラーを隠す

    1
    
    find / -name "*.conf" 2>/dev/null
    
  • -execの終端: \;+の違い

    1
    2
    
    find . -exec cmd {} \;  # 各ファイルごとに実行(遅い)
    find . -exec cmd {} +   # まとめて実行(速い)
    
  • maxdepthで深さ制限: 検索範囲を制限して高速化

    1
    
    find . -maxdepth 2 -name "*.txt"
    

まとめ

今回はfindコマンドについて解説しました。

ポイント:

  • findでファイル・ディレクトリを様々な条件で検索できる
  • -nameでファイル名、-typeでタイプ、-sizeでサイズ指定
  • -mtimeで更新日時による検索が可能
  • -execで見つけたファイルに対してコマンド実行
  • -deleteで見つけたファイルを削除(要注意)
  • 複数条件を組み合わせて詳細な検索が可能

findは超強力ですが、使い方を間違えると大変なことになります。特に-delete-exec rmを使う時は、必ず-printで確認してから実行しましょう。

次回もLinuxコマンドの学習を続けていきましょう!