git lfsに含まれていないファイルの中でデカいファイルを探すbash script
コメントからChatGPTと一緒に育ててきたのがよくわかる
git-large-files.sh
#!/bin/bash
# Gitで管理されているが、LFSには含まれていないファイルを見つけ、サイズの小さい順に表示するスクリプト(Mac対応、numfmtなし)
# 一時ファイルを作成
temp_file=$(mktemp)
temp_lfs=$(mktemp)
# Git LFSで管理されているファイルを一時ファイルにリストアップ
git lfs ls-files --name-only > "$temp_lfs"
# Gitで管理されているファイルをループ
git ls-files | while read file; do
# ファイルがLFSに含まれていなければ、サイズと共に一時ファイルに出力
if ! grep -Fxq "$file" "$temp_lfs"; then
# Macのstatを使ってファイルサイズをバイト単位で取得
echo "$(stat -f%z "$file") $file" >> "$temp_file"
fi
done
# ファイルサイズでソートして逆順に出力
sort -nr "$temp_file" | tail -r | while read -r size file; do
# 人が読みやすいファイルサイズ形式に変換(小数点1桁まで表示)
if [ "$size" -lt 1024 ]; then
b_size=$(echo "scale=1; $size" | bc)
echo "$b_size.0 B: $file"
elif [ "$size" -lt $((1024*1024)) ]; then
kb_size=$(echo "scale=1; $size/1024" | bc)
echo "$kb_size KB: $file"
elif [ "$size" -lt $((1024*1024*1024)) ]; then
mb_size=$(echo "scale=1; $size/1024/1024" | bc)
echo "$mb_size MB: $file"
else
gb_size=$(echo "scale=1; $size/1024/1024/1024" | bc)
echo "$gb_size GB: $file"
fi
done
# 一時ファイルを削除
rm "$temp_file" "$temp_lfs"