Skip to content

Instantly share code, notes, and snippets.

@halfnibble
Last active July 16, 2025 07:31
Show Gist options
  • Save halfnibble/ca17d5eacd91786b76a67d9caca47e9f to your computer and use it in GitHub Desktop.
Save halfnibble/ca17d5eacd91786b76a67d9caca47e9f to your computer and use it in GitHub Desktop.
Count Language LOC
#!/bin/bash
# Usage: ./script.sh [directory]
# If no directory is provided, it uses the current directory.
dir="${1:-.}"
# Common directories to exclude (3rd party libraries and build artifacts)
exclude_dirs=( -name node_modules -o -name Pods -o -name Carthage -o -name build -o -name .build -o -name .gradle -o -name venv -o -name .venv -o -name env )
# Count lines for Swift files (*.swift)
swift_lines=$(find "$dir" -type d \( "${exclude_dirs[@]}" \) -prune -o -type f -name "*.swift" -exec cat {} + 2>/dev/null | wc -l | awk '{print $1}')
if [ -z "$swift_lines" ]; then swift_lines=0; fi
# Count lines for Kotlin files (*.kt)
kotlin_lines=$(find "$dir" -type d \( "${exclude_dirs[@]}" \) -prune -o -type f -name "*.kt" -exec cat {} + 2>/dev/null | wc -l | awk '{print $1}')
if [ -z "$kotlin_lines" ]; then kotlin_lines=0; fi
# Count lines for Python files (*.py)
python_lines=$(find "$dir" -type d \( "${exclude_dirs[@]}" \) -prune -o -type f -name "*.py" -exec cat {} + 2>/dev/null | wc -l | awk '{print $1}')
if [ -z "$python_lines" ]; then python_lines=0; fi
# Count lines for Typescript files (*.ts)
typescript_lines=$(find "$dir" -type d \( "${exclude_dirs[@]}" \) -prune -o -type f -name "*.ts" -exec cat {} + 2>/dev/null | wc -l | awk '{print $1}')
if [ -z "$typescript_lines" ]; then typescript_lines=0; fi
echo "Swift: $swift_lines lines"
echo "Kotlin: $kotlin_lines lines"
echo "Python: $python_lines lines"
echo "Typescript: $typescript_lines lines"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment