Skip to content

Instantly share code, notes, and snippets.

@rchatham
Created January 27, 2024 23:56
Show Gist options
  • Save rchatham/0a948ccb8ef64f99a22bac4273540260 to your computer and use it in GitHub Desktop.
Save rchatham/0a948ccb8ef64f99a22bac4273540260 to your computer and use it in GitHub Desktop.
Lines Of Code
#!/bin/bash
# Check if both directory and extension arguments are provided
if [ $# -ne 2 ]; then
echo "Usage: ./script.sh [directory] [file extension]"
exit 1
fi
dir=$1
ext=$2
total_lines=0
# find files with the given extension and count the lines
while IFS= read -r file; do
line_count=$(wc -l < "$file")
echo "$file: $line_count"
total_lines=$((total_lines + line_count))
done < <(find "$dir" -name "*.$ext")
echo "Total lines in .$ext files: $total_lines"
@rchatham
Copy link
Author

Drop this in your usr/local/bin on Mac, remove the .sh and chmod +x.

Usage looks like loc path/to/directory file_extension to print out total lines of code of that file type in the directory.

@rchatham
Copy link
Author

Example usage:

$ loc OpenAI/Sources swift
OpenAI/Sources/OpenAI/OpenAI.swift:      179
OpenAI/Sources/OpenAI/OpenAI+ChatCompletion.swift:      453
OpenAI/Sources/OpenAI/OpenAI+Assistant.swift:      103
Total lines in .swift files: 735

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