Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save julienreszka/f54ea4c0c278def07b310f9940d29f4d to your computer and use it in GitHub Desktop.
Save julienreszka/f54ea4c0c278def07b310f9940d29f4d to your computer and use it in GitHub Desktop.
Bash script to Find all files (except for ignored extensions) and print their line count, sorted by count
#!/bin/bash
# Find all files (except for ignored extensions) and print their line count, sorted by count
IGNORED_EXTENSIONS=(svg json) # list of extensions to ignore
LINE_THRESHOLD=300
find ./src -type f | while read FILE; do
# Check if the file extension is in the list of ignored extensions
extension="${FILE##*.}"
if [[ "${IGNORED_EXTENSIONS[@]}" =~ "${extension}" ]]; then
continue
fi
# Count the number of lines in the file using grep instead of wc
count=$(grep -c ^ <"$FILE")
# Add the count to the total
((total += count))
# Check if the count is more than threshold, and highlight the filename
if [ $count -gt $LINE_THRESHOLD ]; then
echo "\033[1;31m$count $FILE\033[0m" # highlight the filename in red
else
echo "$count $FILE"
fi
done | sort -n -r -k1 # Sort by count, descending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment