Created
September 30, 2025 11:03
-
-
Save neelabalan/0b3db220460e1d4ad62d6cbefce8c877 to your computer and use it in GitHub Desktop.
Check go file distribution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For - https://github.com/golang/go/tree/go1.24.0/src