Zählen Sie die Summe der Ausgabe des Befehls „wc -l“

Zählen Sie die Summe der Ausgabe des Befehls „wc -l“

ich habe einen Arbeitsbereichsbaum wie:

/Directory
  /Dir1/file1, file2
  /Dir2/file3, file4
  /Dir3/file5, file6
...

Ich möchte die Summe der Zeilenanzahl für jede Datei im Verzeichnis zählen.

Ich habe dieses Skript, aber es zählt nur die Anzahl der Zeilen für jede Datei und nicht die Summe:

#!/bin/bash

find . -maxdepth 1 -mindepth 1 -type d | while read dir; do
  printf "%-25.25s : " "$dir"
  find "$dir" -type f | while read file; do
      linecount= cat $file | wc -l 
      echo "this file contains $linecount lnes"
  done 
done

Antwort1

Meine Lösung wäre:

for d in */; do
    echo -n "$d : "
    sum=0
    for f in "$d"/*; do
        if [ -f "$f" ] ; then
            lines=$(wc -l "$f")
            sum=$((sum+lines))
        fi
    done
    echo $sum
done

Für den Anfänger vielleicht etwas verständlicher.

Antwort2

Minimieren der Anzahl der wcAufrufe:

find /Directory -type d -print0 | while read -d '' dir; do
    echo -n "$dir: "
    find "$dir" -type f -exec wc -l {} + | sed -n 's/\([0-9]\{1,\}\) total/\1/p' | paste -sd+ | bc
done

verwandte Informationen