The Curiosity
I've been writing code for years, accumulating numerous repositories on GitHub that I rarely revisit. One day, a thought struck me: "Just how many lines of code have I actually written?"
It's not about measuring productivity or comparing myself to others. The number itself doesn't really matter. It's more like flipping through an old photo album—you're not trying to achieve anything, just curious about the journey.
The Tool: cloc
I discovered cloc, a command-line utility that analyzes source code and produces statistics:
- Number of files per language
- Lines of code, comments, and blanks per language
- Clean, formatted output
Example output looks like this:
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Python 45 1234 567 8901
JavaScript 32 876 234 5678
...
-------------------------------------------------------------------------------
The problem? All my code lives on GitHub. Cloning repositories one by one to run cloc locally would be tedious and time-consuming.
The Solution: Automate Everything
I wrote a bash script that handles the entire workflow:
- Fetches all repositories from my GitHub account using the
ghCLI - Syncs each repository locally (clones new ones, pulls updates for existing)
- Runs
clocon the entire directory, excluding build artifacts and dependencies
Prerequisites
Install the required tools on your system:
macOS:
brew install gh jq cloc git
Ubuntu/Debian:
sudo apt update && sudo apt install gh jq cloc git
Windows (WSL):
wsl --install
Then within Ubuntu:
sudo apt update && sudo apt install gh jq cloc git
Important: Authenticate with GitHub CLI before running:
gh auth login
This opens a browser for one-time OAuth authorization.
The Script
#!/usr/bin/env bash
GITHUB_USER="your_username" # Replace with your GitHub username
STORAGE_DIR="$HOME/projects_backup"
# Initialize storage directory
mkdir -p "$STORAGE_DIR"
cd "$STORAGE_DIR" || exit
echo "Fetching all repositories for $GITHUB_USER..."
# Retrieve repository URLs and process each one
gh repo list "$GITHUB_USER" --limit 500 --json nameWithOwner,url \
| jq -r '.[].url' | while IFS= read -r repo_url; do
local_name=$(basename "$repo_url")
if [ -d "$local_name" ]; then
echo "Updating existing: $local_name"
cd "$local_name" && git pull --quiet && cd "$STORAGE_DIR"
else
echo "Cloning new repository: $local_name"
git clone --depth 1 "$repo_url" --quiet
fi
done
echo ""
echo "Computing code statistics (excluding vendor, node_modules, dist, build)..."
echo ""
cloc --exclude-dir=node_modules,vendor,dist,build,public,target,.git \
--exclude-lang=JSON,SVG "$STORAGE_DIR"
The --depth 1 flag performs shallow clones, keeping disk usage minimal since we only need the latest code.
Sample Results
Running the script produces output like:
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
PHP 2334 14079 60356 613174
Vuejs Component 232 2273 1207 29480
JavaScript 198 2769 2813 16292
CSS 13 1566 182 13460
YAML 15 3345 1 12314
HTML 43 507 262 11776
Markdown 52 1881 11 4773
Blade 27 386 52 3942
Go 4 80 65 887
...
-------------------------------------------------------------------------------
SUM: 3303 27162 65119 708414
-------------------------------------------------------------------------------
Simplifying for Local Projects
If your code is already on disk, skip the GitHub synchronization entirely. Just navigate to any directory and run:
cloc .
That's it—no scripts needed.
Summary
clocprovides quick, detailed code statistics across multiple languages- The
ghCLI enables programmatic access to GitHub repositories - Combining these tools with
gitcreates a fully automated statistics pipeline - While the metric itself is meeningless, it's surprisingly nostalgic to see