locate データベース検索
locate
は事前に作成されたデータベースを使用した高速なファイル検索ツールです。
locate の基本概念
CommandAcademy Terminal
Welcome to CommandAcademy Terminal!
Type "help" to see available commands.
user@cmdac:~$
█
ファイルツリー
/
etc
hosts35B
passwd76B
home
user
tmp
usr
bin
share
var
log
locate の特徴:
- 非常に高速(インデックスベース検索)
- ファイルシステム全体を対象
- 定期的なデータベース更新が必要
- パス名での部分マッチング
- 正規表現サポート
locate の使用方法
# 基本的な検索
locate example
# 大文字小文字を区別しない
locate -i EXAMPLE
# 正規表現を使用
locate -r '\.txt$'
# 結果数を制限
locate -l 10 example
# 統計情報を表示
locate -S
# データベースの手動更新
sudo updatedb
# 特定のデータベースを使用
locate -d /custom/db/path filename
データベースの管理とカスタマイズ
CommandAcademy Terminal
Welcome to CommandAcademy Terminal!
Type "help" to see available commands.
user@cmdac:~$
█
ファイルツリー
/
etc
hosts35B
passwd76B
home
user
tmp
usr
bin
share
var
log
# データベース設定ファイルの確認
cat /etc/updatedb.conf
# カスタムデータベースの作成
mkdir -p ~/.local/share/locate
updatedb -l 0 -o ~/.local/share/locate/custom.db -U ~/important_files
# カスタムデータベースで検索
locate -d ~/.local/share/locate/custom.db pattern
# 除外パターンの設定
# /etc/updatedb.conf に以下を追加:
# PRUNEPATHS="/tmp /var/tmp /var/cache"
# PRUNEFS="tmpfs devtmpfs"
# 即座にデータベースを更新
sudo updatedb --localpaths=/home/user/projects
find の高度な条件式
find の複雑な条件組み合わせを学びます。
複数条件の論理演算
CommandAcademy Terminal
Welcome to CommandAcademy Terminal!
Type "help" to see available commands.
user@cmdac:~$
█
ファイルツリー
/
etc
hosts35B
passwd76B
home
user
tmp
usr
bin
share
var
log
# AND条件(デフォルト)
find test_find -name "*.txt" -size +1k
# OR条件
find test_find \( -name "*.txt" -o -name "*.pdf" \)
# NOT条件
find test_find -type f ! -name "*.tmp"
# 複雑な条件組み合わせ
find test_find \( -name "*.py" -o -name "*.js" \) -a ! -path "*/node_modules/*"
# 括弧を使った優先順位制御
find test_find \( -name "*.log" -a -size +10M \) -o \( -name "*.tmp" -a -mtime +7 \)
時間ベースの高度な検索
# 特定の時間範囲
find . -newermt "2024-01-01" ! -newermt "2024-02-01"
# 相対的な時間指定
find . -mtime 0 # 今日変更されたファイル
find . -mtime +7 # 7日より前
find . -mtime -7 # 7日以内
# アクセス時間、変更時間、inode変更時間
find . -atime -1 # 1日以内にアクセス
find . -ctime -1 # 1日以内にinode変更
# より精密な時間指定
find . -mmin -60 # 60分以内に変更
find . -newer /tmp/reference_file
サイズとパーミッションの詳細検索
CommandAcademy Terminal
Welcome to CommandAcademy Terminal!
Type "help" to see available commands.
user@cmdac:~$
█
ファイルツリー
/
etc
hosts35B
passwd76B
home
user
tmp
usr
bin
share
var
log
# 詳細なサイズ指定
find . -size +100M -size -1G # 100MB以上1GB未満
find . -size 0 # 空ファイル
find . -size +0 # 空でないファイル
# パーミッションの詳細検索
find . -perm 644 # 正確に644
find . -perm -644 # 644以上の権限
find . -perm /644 # いずれかのビットが立っている
# 実行可能ファイル
find . -perm -111 # すべてに実行権限
find . -perm /111 # いずれかに実行権限
# 特定の所有者とグループ
find . -user alice -group developers
find . -uid 1000 -gid 100
検索結果の高度な処理
xargs との高度な組み合わせ
CommandAcademy Terminal
Welcome to CommandAcademy Terminal!
Type "help" to see available commands.
user@cmdac:~$
█
ファイルツリー
/
etc
hosts35B
passwd76B
home
user
tmp
usr
bin
share
var
log
# null文字区切りで安全に処理
find . -name "*.txt" -print0 | xargs -0 grep -l "sample"
# 並列処理
find . -name "*.jpg" -print0 | xargs -0 -P 4 -I {} jpegoptim {}
# 複雑なコマンド実行
find . -name "*.py" -print0 | xargs -0 -I {} sh -c 'echo "Processing {}" && python -m py_compile {}'
# 条件分岐付き処理
find . -name "*.log" -print0 | while IFS= read -r -d '' file; do
if [ -s "$file" ]; then
gzip "$file"
else
rm "$file"
fi
done
exec の高度な使用法
# 複数コマンドの実行
find . -name "*.tmp" -exec sh -c 'echo "Removing $1" && rm "$1"' _ {} \;
# 条件付きexec
find . -type f -exec test -s {} \; -exec echo "Non-empty: {}" \;
# ディレクトリ単位での処理
find . -type d -execdir pwd \;
# バックアップ付き操作
find . -name "*.conf" -exec cp {} {}.bak \; -exec sed -i 's/old/new/g' {} \;
grep との組み合わせテクニック
ファイル内容とメタデータの組み合わせ検索
CommandAcademy Terminal
Welcome to CommandAcademy Terminal!
Type "help" to see available commands.
user@cmdac:~$
█
ファイルツリー
/
etc
hosts35B
passwd76B
home
user
tmp
usr
bin
share
var
log
# 特定の内容を含む大きなファイル
find . -size +10k -exec grep -l "pattern" {} \;
# 最近変更された特定の内容のファイル
find . -mtime -7 -name "*.py" -exec grep -H "import" {} \;
# 複数パターンの複合検索
find . -name "*.log" -exec sh -c 'grep -q "ERROR" "$1" && grep -c "WARNING" "$1" > 10 && echo "$1"' _ {} \;
# ファイル名とコンテンツの両方でマッチ
find . -name "*config*" -exec grep -l "database" {} \;
正規表現を使った高度な検索
# 複雑なファイル名パターン
find . -regex '.*/[0-9]{4}-[0-9]{2}-[0-9]{2}.*\.log'
# 部分マッチ vs 完全マッチ
find . -name "*test*" # 部分マッチ
find . -regex '.*/test[^/]*$' # より厳密なパターン
# 拡張子の複雑なパターン
find . -regex '.*\.\(jpg\|png\|gif\)$' -iname
パフォーマンス最適化
検索効率の改善
CommandAcademy Terminal
Welcome to CommandAcademy Terminal!
Type "help" to see available commands.
user@cmdac:~$
█
ファイルツリー
/
etc
hosts35B
passwd76B
home
user
tmp
usr
bin
share
var
log
# 深さ制限で高速化
find . -maxdepth 2 -name "*.txt"
# 特定のディレクトリをスキップ
find . -path "./node_modules" -prune -o -name "*.js" -print
# ファイルシステムを跨がない
find . -mount -name "*.log"
# 早期終了
find . -name "target.txt" -quit
# 複数の除外パターン
find . \( -path "./tmp" -o -path "./cache" -o -path "./.git" \) -prune -o -type f -print
find の代替ツール
# fd(modern find alternative)- より高速で使いやすい
fd "pattern" # 基本検索
fd -t f "pattern" # ファイルのみ
fd -t d "pattern" # ディレクトリのみ
fd -e txt # 拡張子で検索
fd -H "pattern" # 隠しファイルも含む
fd -I "pattern" # .gitignore を無視
# ripgrep(rg)- grep の高速版
rg "pattern" --type py # Python ファイル内検索
rg -i "pattern" # 大文字小文字無視
rg -w "word" # 単語境界
rg "pattern" --files-with-matches # ファイル名のみ表示
実践演習
演習1: 複雑な条件検索
CommandAcademy Terminal
Welcome to CommandAcademy Terminal!
Type "help" to see available commands.
user@cmdac:~$
█
ファイルツリー
/
etc
hosts35B
passwd76B
home
user
tmp
usr
bin
share
var
log
以下の条件を満たすファイルを検索してください:
- プロジェクト内のJavaScriptファイル
- node_modulesディレクトリを除く
- 1KB以上のサイズ
- 過去30日以内に変更
解答例:
find project -name "*.js" -path "*/node_modules" -prune -o -type f -size +1k -mtime -30 -print
# または fd を使用
fd -e js -s +1k --changed-within 30d project --exclude node_modules
演習2: 内容ベースの検索とクリーンアップ
CommandAcademy Terminal
Welcome to CommandAcademy Terminal!
Type "help" to see available commands.
user@cmdac:~$
█
ファイルツリー
/
etc
hosts35B
passwd76B
home
user
tmp
usr
bin
share
var
log
- "TODO"または"FIXME"を含むPythonファイルを見つけてください
- それらのファイルの行番号とコンテンツを表示
- 見つかったファイルをtodo_listディレクトリにコピー
解答例:
# TODO/FIXMEを含むファイルを検索
find cleanup -name "*.py" -exec grep -l -E "(TODO|FIXME)" {} \; > todo_files.txt
# 行番号付きで内容表示
cat todo_files.txt | xargs grep -n -E "(TODO|FIXME)"
# ファイルをコピー
mkdir -p todo_list
cat todo_files.txt | xargs -I {} cp {} todo_list/
# または一行で
find cleanup -name "*.py" -exec grep -l -E "(TODO|FIXME)" {} \; -exec cp {} todo_list/ \;
検索パターンのライブラリ
よく使う検索パターン集
# 開発関連
# 大きなログファイル
find /var/log -name "*.log" -size +100M
# 最近のエラーログ
find /var/log -name "*.log" -mtime -1 -exec grep -l ERROR {} \;
# 重複ファイル候補
find . -type f -exec md5sum {} \; | sort | uniq -d -w32
# 壊れたシンボリックリンク
find . -type l -exec test ! -e {} \; -print
# setuidファイル(セキュリティ監査)
find / -perm -4000 -type f
# 空のディレクトリ
find . -type d -empty
# 最大サイズのファイルTop10
find . -type f -exec ls -la {} \; | sort -k5 -nr | head -10
システム管理パターン
# キャッシュクリーンアップ
find /tmp -type f -atime +7 -delete
find ~/.cache -type f -mtime +30 -delete
# コアダンプファイル
find /var -name "core" -type f -mtime +7
# 権限の問題があるファイル
find /var/www -type f ! -perm -644 -exec ls -l {} \;
# 未使用のアーカイブファイル
find /backup -name "*.tar.gz" -atime +90
# 巨大なファイルの特定
find / -xdev -type f -size +1G 2>/dev/null
デバッグとトラブルシューティング
検索の問題解決
# パーミッション拒否を除外
find / -name "pattern" 2>/dev/null
# 詳細な実行ログ
find . -name "*.txt" -ls
# 実際に実行せずテスト
find . -name "*.tmp" -ok rm {} \;
# 複雑な条件式のデバッグ
find . -name "*.log" -print -o -name "*.txt" -print # それぞれ何が見つかったか確認
# find の内部動作を表示
strace -e trace=file find . -name "pattern" 2>&1 | grep open
検索結果の検証
# 結果の統計
find . -type f | wc -l # ファイル総数
find . -type d | wc -l # ディレクトリ総数
# サイズの統計
find . -type f -exec ls -l {} \; | awk '{total += $5} END {print "Total:", total}'
# 結果の詳細分析
find . -type f -name "*.txt" -printf "%s %p\n" | sort -n
まとめ
このレッスンでは、高度なファイル検索テクニックを学びました:
- locate データベースの効率的な活用
- find の複雑な条件式構築
- 検索結果の高度な処理方法
- パフォーマンスを考慮した検索戦略
- 実践的な検索パターンライブラリ
これらの技術を組み合わせることで、大規模なファイルシステムでも効率的にファイルを見つけ出せるようになります。
💡 次のレッスンでは、学習したファイル操作技術を使った総合演習を行います。