Created
January 27, 2024 23:56
-
-
Save rchatham/0a948ccb8ef64f99a22bac4273540260 to your computer and use it in GitHub Desktop.
Lines Of Code
This file contains 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 | |
# 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" |
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
Drop this in your
usr/local/bin
on Mac, remove the.sh
andchmod +x
.Usage looks like
loc path/to/directory file_extension
to print out total lines of code of that file type in the directory.