LLI Wiki — My Digital Knowledge Base

Shell: count lines per file in a directory

Bash script that prints the line count of each file in a directory with a running total, and its limits with complex file names.

A bash script that counts the lines of each file in a directory and prints a running total.

#!/bin/bash

total=0;
find data | sort | while read in;
do
  if [ -f "$in" ]; then
    count=$(wc -l $in | cut -d' ' -f1);
    total=$((total + count));
    echo -n "[*] $in -               " | cut -z -b1-22
    echo -n "$count                  " | cut -z -b1-10
    echo    "(total: $total)";
  fi;
done

The script walks the data directory (change it as needed), skips anything that is not a regular file, and prints the line count of each file together with the running total.

Notes

Mastodon LLIMastodon SocialFosstodon