Skip to content

Instantly share code, notes, and snippets.

@neelabalan
Created September 30, 2025 11:03
Show Gist options
  • Save neelabalan/0b3db220460e1d4ad62d6cbefce8c877 to your computer and use it in GitHub Desktop.
Save neelabalan/0b3db220460e1d4ad62d6cbefce8c877 to your computer and use it in GitHub Desktop.
Check go file distribution
#!/bin/bash
sort_by="files"
target_dir="."
while [[ $# -gt 0 ]]; do
case $1 in
-l|--lines)
sort_by="lines"
shift
;;
-f|--files)
sort_by="files"
shift
;;
-h|--help)
echo "Usage: $0 [OPTIONS] [DIRECTORY]"
echo "Options:"
echo " -f, --files Sort by number of files (default)"
echo " -l, --lines Sort by number of lines"
echo " -h, --help Show this help"
exit 0
;;
*)
target_dir="$1"
shift
;;
esac
done
if [ ! -d "$target_dir" ]; then
echo "Error: Directory '$target_dir' does not exist"
exit 1
fi
echo "Go files analysis for: $target_dir (sorted by $sort_by)"
echo "================================================"
printf "%-20s %10s %15s\n" "Directory" "Go Files" "Total Lines"
echo "------------------------------------------------"
total_files=0
total_lines=0
temp_file=$(mktemp)
for item in "$target_dir"/*; do
if [ -d "$item" ]; then
dir_name=$(basename "$item")
file_count=$(find "$item" -name "*.go" -type f | wc -l)
if [ "$file_count" -gt 0 ]; then
line_count=$(find "$item" -name "*.go" -type f -exec cat {} \; | wc -l)
echo "$dir_name $file_count $line_count" >> "$temp_file"
total_files=$((total_files + file_count))
total_lines=$((total_lines + line_count))
fi
fi
done
root_files=$(find "$target_dir" -maxdepth 1 -name "*.go" -type f | wc -l)
if [ "$root_files" -gt 0 ]; then
root_lines=$(find "$target_dir" -maxdepth 1 -name "*.go" -type f -exec cat {} \; | wc -l)
echo "(root) $root_files $root_lines" >> "$temp_file"
total_files=$((total_files + root_files))
total_lines=$((total_lines + root_lines))
fi
if [ "$sort_by" = "lines" ]; then
sort -k3 -nr "$temp_file" | while read -r dir files lines; do
printf "%-20s %10d %15d\n" "$dir" "$files" "$lines"
done
else
sort -k2 -nr "$temp_file" | while read -r dir files lines; do
printf "%-20s %10d %15d\n" "$dir" "$files" "$lines"
done
fi
rm -f "$temp_file"
echo "================================================"
printf "%-20s %10d %15d\n" "TOTAL" "$total_files" "$total_lines"
@neelabalan
Copy link
Author

For - https://github.com/golang/go/tree/go1.24.0/src

bash-3.2$ ./go-check -l . | head -10
Go files analysis for: . (sorted by lines)
================================================
Directory              Go Files     Total Lines
------------------------------------------------
cmd                        2507         1231259
syscall                     302          170476
runtime                     766          166979
net                         378          134871
crypto                      445          124029
vendor                      164          107477
bash-3.2$ ./go-check . | head -10
Go files analysis for: . (sorted by files)
================================================
Directory              Go Files     Total Lines
------------------------------------------------
cmd                        2507         1231259
internal                   1016          103112
runtime                     766          166979
crypto                      445          124029
net                         378          134871
go                          313           77586

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment