「あのファイル、どこに保存したっけ…」 「特定の拡張子のファイルを全部探したい」 「最近更新されたファイルだけを見つけたい」
こんな悩みを抱えていませんか?
Linuxには強力なファイル検索コマンドが豊富に用意されています。 特にfind
コマンドは、Windowsの検索機能を遥かに超える柔軟性と速度を誇るんです。
この記事では、基本的な検索から高度な条件指定まで、実例たっぷりで解説します。 もうファイルを見失うことはありません!
最速!3秒でファイルを見つける基本コマンド

find:最強の検索コマンド
ファイル名で検索(最も基本的な使い方):
# カレントディレクトリ以下で検索
find . -name "ファイル名"
# 例:test.txtを探す
find . -name "test.txt"
# ホームディレクトリ全体で検索
find ~ -name "test.txt"
# システム全体で検索(時間がかかる)
sudo find / -name "test.txt"
ワイルドカードを使った検索
# .txtファイルをすべて検索
find . -name "*.txt"
# testで始まるファイル
find . -name "test*"
# 3文字の.shファイル
find . -name "???.sh"
# 大文字小文字を区別しない検索
find . -iname "TEST.txt" # test.txt, TEST.txt, Test.txt すべてヒット
locate:超高速データベース検索
locateは事前にインデックス化されたDBを検索するので爆速!
# インストール(必要な場合)
sudo apt install mlocate # Ubuntu/Debian
sudo yum install mlocate # CentOS/RHEL
# データベースを更新
sudo updatedb
# 検索実行
locate test.txt
# パスの一部でも検索可能
locate /home/user/Documents
# ワイルドカード使用
locate "*.pdf"
# 大文字小文字を区別しない
locate -i readme
locateの特徴:
- ✅ 超高速(データベース検索のため)
- ✅ システム全体を一瞬で検索
- ❌ 最新のファイルは見つからない可能性
- ❌ 削除済みファイルが表示される可能性
ファイル名検索の実践テクニック
複数の名前パターンで検索
# OR条件:.txt または .log ファイル
find . -name "*.txt" -o -name "*.log"
# より複雑な条件
find . \( -name "*.jpg" -o -name "*.png" -o -name "*.gif" \) -type f
# 除外条件:.gitディレクトリを除外して検索
find . -name "*.js" -not -path "*/\.git/*"
# node_modulesを除外(開発者向け)
find . -name "*.json" -not -path "*/node_modules/*"
正規表現で高度な検索
# -regexオプションで正規表現を使用
find . -regex ".*\.[jt]s$" # .js または .ts ファイル
# 数字を含むファイル名
find . -regex ".*[0-9]+.*"
# 日付形式のファイル名(例:2024-01-15.log)
find . -regex ".*/[0-9]{4}-[0-9]{2}-[0-9]{2}\.log"
ファイルタイプで検索
種類別の検索
# ファイルのみ検索
find . -type f -name "*.txt"
# ディレクトリのみ検索
find . -type d -name "config"
# シンボリックリンクを検索
find . -type l
# 実行可能ファイルを検索
find . -type f -executable
# 空のファイルを検索
find . -type f -empty
# 空のディレクトリを検索
find . -type d -empty
権限で検索
# 特定の権限を持つファイル
find . -perm 755
# 書き込み可能なファイル
find . -perm -222
# SUIDビットが設定されたファイル(セキュリティ監査)
find / -perm -4000 2>/dev/null
# 誰でも書き込み可能な危険なファイル
find . -perm -002 -type f
サイズで検索
ファイルサイズ指定
# 1MB以上のファイル
find . -size +1M
# 100KB未満のファイル
find . -size -100k
# ちょうど1GBのファイル
find . -size 1G
# 10MB〜100MBのファイル
find . -size +10M -size -100M
# サイズ単位:
# c = バイト
# k = キロバイト
# M = メガバイト
# G = ギガバイト
大容量ファイルを探す実用例
# ホームディレクトリで100MB以上のファイルを探してサイズ順に表示
find ~ -type f -size +100M -exec ls -lh {} \; | sort -k5 -rh
# ディスク容量を圧迫しているファイルTOP10
find . -type f -exec du -h {} + | sort -rh | head -10
# 1GB以上の動画ファイルを探す
find . -type f -size +1G \( -name "*.mp4" -o -name "*.avi" -o -name "*.mkv" \)
日時で検索
更新日時での検索
# 7日以内に変更されたファイル
find . -mtime -7
# 30日以上前に変更されたファイル
find . -mtime +30
# ちょうど1日前に変更されたファイル
find . -mtime 1
# 1時間以内に変更されたファイル
find . -mmin -60
# 24時間以内に作成された新しいファイル
find . -type f -newermt "24 hours ago"
アクセス日時・作成日時での検索
# 3日以内にアクセスされたファイル
find . -atime -3
# 作成日時で検索(ファイルシステムによる)
find . -ctime -7
# 特定の日時以降に変更されたファイル
find . -newermt "2024-01-01"
# 2つの日時の間に変更されたファイル
find . -newermt "2024-01-01" ! -newermt "2024-01-31"
タイムスタンプ比較
# reference.txtより新しいファイル
find . -newer reference.txt
# reference.txtより古いファイル
find . ! -newer reference.txt
# 2つのファイルの間の時刻のファイル
find . -newer file1.txt ! -newer file2.txt
所有者・グループで検索
ユーザー・グループ指定
# 特定ユーザーのファイル
find . -user username
# 特定グループのファイル
find . -group groupname
# rootユーザーのファイル
find / -user root
# 存在しないユーザーのファイル(削除されたユーザー)
find . -nouser
# 存在しないグループのファイル
find . -nogroup
検索結果に対するアクション
-execで見つけたファイルに処理を実行
# 見つけたファイルを削除
find . -name "*.tmp" -exec rm {} \;
# より高速な削除(+を使用)
find . -name "*.tmp" -exec rm {} +
# 見つけたファイルを別ディレクトリにコピー
find . -name "*.pdf" -exec cp {} /backup/ \;
# 見つけたファイルの詳細情報を表示
find . -name "*.log" -exec ls -la {} \;
# 見つけたファイルの中身を検索
find . -name "*.txt" -exec grep "検索文字列" {} \;
# 見つけたファイルの権限を変更
find . -type f -name "*.sh" -exec chmod +x {} \;
-okで確認しながら実行
# 削除前に確認
find . -name "*.bak" -ok rm {} \;
# 各ファイルごとに確認メッセージが表示される
xargsと組み合わせる
# より効率的な処理
find . -name "*.txt" | xargs grep "検索文字列"
# スペースを含むファイル名に対応
find . -name "*.txt" -print0 | xargs -0 grep "検索文字列"
# 並列処理で高速化
find . -name "*.jpg" -print0 | xargs -0 -P 4 -I {} convert {} -resize 50% small_{}
ファイル内容の検索(grep連携)

ファイルの中身を検索
# カレントディレクトリ以下のすべてのファイルから検索
grep -r "検索文字列" .
# 特定の拡張子のファイルのみ検索
grep -r "TODO" --include="*.py"
# 大文字小文字を区別しない
grep -ri "error" .
# 行番号付きで表示
grep -rn "function" .
# 検索結果のファイル名のみ表示
grep -rl "password" .
findとgrepの組み合わせ
# 特定の条件のファイルから内容検索
find . -name "*.log" -mtime -7 -exec grep "ERROR" {} +
# より詳細な結果表示
find . -name "*.conf" -exec grep -H "server" {} \;
高速化テクニック
fd:findの高速代替コマンド
# インストール
sudo apt install fd-find # Ubuntu/Debian
sudo dnf install fd-find # Fedora
# 使い方(超シンプル)
fd test # testを含むファイル/ディレクトリ
fd -e txt # .txtファイルのみ
fd -t f test # testを含むファイルのみ
fd -t d config # configを含むディレクトリのみ
ripgrep (rg):超高速grep
# インストール
sudo apt install ripgrep
# 使い方
rg "検索文字列" # カレントディレクトリ以下を高速検索
rg -t py "import" # Pythonファイルのみ検索
rg -C 3 "error" # マッチした行の前後3行も表示
検索を高速化するコツ
# 1. 検索範囲を限定
find ./src -name "*.js" # /でなく特定ディレクトリから
# 2. 深さを制限
find . -maxdepth 3 -name "*.txt"
# 3. 不要なディレクトリを除外
find . -path ./node_modules -prune -o -name "*.js" -print
# 4. 並列処理を活用
find . -name "*.log" | parallel grep "ERROR" {}
実用的な検索スクリプト
定期的なクリーンアップスクリプト
#!/bin/bash
# cleanup.sh - 古いファイルを削除
# 30日以上前のログファイルを削除
find /var/log -name "*.log" -mtime +30 -delete
# 7日以上前のtmpファイルを削除
find /tmp -type f -atime +7 -delete
# 空のディレクトリを削除
find . -type d -empty -delete
echo "クリーンアップ完了"
重複ファイル検索スクリプト
#!/bin/bash
# find_duplicates.sh
# MD5ハッシュでファイルの重複を検索
find . -type f -exec md5sum {} + |
sort |
uniq -w32 -d |
while read hash file; do
echo "重複: $file (hash: $hash)"
done
バックアップ対象ファイル検索
#!/bin/bash
# find_backup_targets.sh
# 過去24時間に変更されたファイルをリスト化
find . -type f -mtime -1 > backup_list.txt
# 重要な設定ファイルを追加
find /etc -name "*.conf" -mtime -7 >> backup_list.txt
# 1MB以上の新しいファイルは警告
find . -type f -mtime -1 -size +1M -exec echo "大きなファイル: {}" \;
トラブルシューティング
Permission denied エラーの対処
# エラーメッセージを非表示
find / -name "test.txt" 2>/dev/null
# sudoで実行
sudo find / -name "test.txt"
# アクセス可能な場所のみ検索
find . -name "test.txt" 2>&1 | grep -v "Permission denied"
検索が遅い場合の対策
# locateを使う(事前にupdatedb実行)
locate filename
# fdやrgなど高速ツールを使用
fd filename
rg "content"
# ioniceで優先度を下げて実行
ionice -c3 find / -name "*.log"
シンボリックリンクの扱い
# シンボリックリンクを辿る
find -L . -name "*.txt"
# シンボリックリンクを辿らない(デフォルト)
find -P . -name "*.txt"
# シンボリックリンク自体を検索
find . -type l
よくある質問(FAQ)

Q1:findとlocateどちらを使うべき?
A:用途によります
- locate:システム全体を素早く検索したい時
- find:最新の状態を確実に検索したい時、複雑な条件を指定したい時
Q2:大量のファイルから効率的に検索するには?
A:以下の順番で試してください
fd
またはrg
を使う(最速)locate
を使う(データベース検索)find
with-maxdepth
で深さ制限- 通常の
find
Q3:Windowsのように拡張子で検索するには?
A:簡単です
# すべての画像ファイル
find . \( -name "*.jpg" -o -name "*.png" -o -name "*.gif" \)
# または fd を使って
fd -e jpg -e png -e gif
Q4:GUIでファイル検索したい
A:ファイルマネージャーの検索機能を使用
- Nautilus(GNOME):Ctrl+F
- Dolphin(KDE):Ctrl+F
- Thunar(XFCE):Ctrl+F
まとめ:用途別おすすめコマンド
Linuxでのファイル検索について、用途別にまとめます:
基本の検索:
- 🔍
find . -name "filename"
– 最も基本的 - ⚡
locate filename
– 高速検索 - 🚀
fd filename
– モダンで高速
条件別検索:
- 📅 日時:
find . -mtime -7
- 📦 サイズ:
find . -size +100M
- 👤 所有者:
find . -user username
- 📝 内容:
grep -r "text" .
実践的な使い方:
# 最近の大きなファイルを探す
find . -type f -mtime -7 -size +10M
# 特定拡張子を一括処理
find . -name "*.txt" -exec command {} \;
# 不要ファイルの削除
find . -name "*.tmp" -delete
高速化のコツ:
- 検索範囲を限定する
- fdやrgなどのモダンツールを使う
- locateでインデックス検索
- 不要なディレクトリを除外
Linuxのファイル検索は非常に強力で柔軟です。 この記事で学んだコマンドを組み合わせれば、どんなファイルも確実に見つけることができます!
コメント