Skip to content

Instantly share code, notes, and snippets.

@floriankraemer
Created July 5, 2023 14:29
Show Gist options
  • Save floriankraemer/a189de162bc0160d0adfe0c50c83cf6a to your computer and use it in GitHub Desktop.
Save floriankraemer/a189de162bc0160d0adfe0c50c83cf6a to your computer and use it in GitHub Desktop.
loc.sh
#!/bin/bash
# Directory to scan
directory="$1"
# Check if directory is provided
if [ -z "$directory" ]; then
echo "Please provide a directory."
exit 1
fi
# Check if directory exists
if [ ! -d "$directory" ]; then
echo "Directory '$directory' does not exist."
exit 1
fi
# Function to recursively count lines in files
count_lines() {
local dir="$1"
# Iterate over each file in the directory
for file in "$dir"/*; do
# Check if file is a regular file
if [ -f "$file" ]; then
# Count the number of lines in the file
local line_count=$(wc -l < "$file")
echo "File: $file, Line count: $line_count"
# Add line count to total
((total_lines += line_count))
elif [ -d "$file" ]; then
# Recursively call the function for subdirectories
count_lines "$file"
fi
done
}
# Initialize total line count
total_lines=0
# Call the function to count lines in files
count_lines "$directory"
# Output the total line count
echo "Total lines of code: $total_lines"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment