file-operations について
Linux のファイルシステム操作を扱うスキルガイドです。検索、一括処理、内容の確認、権限管理を通じて、日常の保守やトラブル対応を支援します。
主な機能
- 01名前や種類によるファイル検索
- 02コピー、移動、一括リネーム
- 03内容の表示・比較・集計
- 04権限と所有者の管理
- 05大容量・最近更新されたファイルの検索
活用シーン
- 01ログや指定種類のファイルを探す
- 02一時ファイルや古いログを一括削除
- 03ディスク使用量を調べ大きなファイルを特定
- 04権限変更や内容の一括置換
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ chaterm/ terminal-skills --skill file-operationsインストール後、Claude Code で/file-operationsを入力して読み込まれたか確認します。
詳細
ファイルとディレクトリの操作
概要
ファイル検索、バッチ操作、権限管理など、Linux ファイルシステムの操作スキルです。
ファイル検索
find コマンド
# Search by name
find /path -name "*.log"
find /path -iname "*.LOG" # Case insensitive
# Search by type
find /path -type f # Files
find /path -type d # Directories
find /path -type l # Symbolic links
# Search by time
find /path -mtime -7 # Modified within 7 days
find /path -mtime +30 # Modified more than 30 days ago
find /path -mmin -60 # Modified within 60 minutes
# Search by size
find /path -size +100M # Larger than 100MB
find /path -size -1k # Smaller than 1KB
# Combined conditions
find /path -name "*.log" -mtime +7 -size +10M
locate コマンド
# Quick search (requires database update)
locate filename
updatedb # Update database
# Case insensitive
locate -i filename
ファイル操作
基本操作
# Copy
cp file1 file2
cp -r dir1 dir2 # Recursive copy directory
cp -p file1 file2 # Preserve attributes
# Move/Rename
mv file1 file2
mv file1 /path/to/dest/
# Delete
rm file
rm -rf dir # Recursive force delete
rm -i file # Interactive confirmation
# Create
touch file # Create empty file
mkdir -p dir1/dir2/dir3 # Recursive create directories
バッチ操作
# Batch rename
rename 's/old/new/' *.txt
for f in *.txt; do mv "$f" "${f%.txt}.md"; done
# Batch delete
find /path -name "*.tmp" -delete
find /path -name "*.log" -mtime +30 -exec rm {} \;
# Batch copy
find /src -name "*.conf" -exec cp {} /dest/ \;
ファイルの内容
ファイルを表示
cat file # Full content
head -n 20 file # First 20 lines
tail -n 20 file # Last 20 lines
tail -f file # Real-time follow
less file # Paginated view
# Statistics
wc -l file # Line count
wc -w file # Word count
wc -c file # Byte count
ファイルを比較
diff file1 file2
diff -u file1 file2 # Unified format
diff -r dir1 dir2 # Compare directories
# Side-by-side comparison
sdiff file1 file2
vimdiff file1 file2
権限管理
権限を確認
ls -la
stat file
権限を変更
# Numeric mode
chmod 755 file # rwxr-xr-x
chmod 644 file # rw-r--r--
# Symbolic mode
chmod u+x file # Add execute for user
chmod g-w file # Remove write for group
chmod o=r file # Set read-only for others
chmod a+r file # Add read for all
# Recursive modify
chmod -R 755 dir
所有者を変更
chown user file
chown user:group file
chown -R user:group dir # Recursive modify
よくあるシナリオ
シナリオ 1:大容量ファイルのクリーンアップ
# Find files larger than 100MB
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null
# Find and sort by size
du -ah /path | sort -rh | head -20
シナリオ 2:最近変更されたファイルを検索
# Files modified within 24 hours
find /path -type f -mtime -1
# Sort by modification time
ls -lt /path | head -20
シナリオ 3:ファイルの内容を一括置換
# Single file replacement
sed -i 's/old/new/g' file
# Batch replacement
find /path -name "*.conf" -exec sed -i 's/old/new/g' {} \;
トラブルシューティング
| 問題 | 解決方法 |
|---|---|
| アクセスが拒否される | sudo を使用するか、ファイルの権限を確認します |
| ディスク容量がいっぱい | 大容量ファイルを見つけるには、df -h、du -sh * を実行します |
| ファイル名に特殊文字が含まれる | 引用符を使用するか、エスケープします。例:rm "file name" |
| 多数のファイルの削除に時間がかかる | rsync --delete または find -delete を使用します |
コメント
Markdown 対応