I have a one-liner script like this that gives a total of everything in various directories:
for i in *;
do (cd $i && cd statelist && echo $i && ls -la |awk 'NR>3 {SUM += $5}\
END { print "Total number of elements " SUM }');done
It works just great but at the end I want to print a grand total of all the numbers in column 5. I tried to tack on another SUM statement but it didn't work. Not sure how this code should work for grand total.
It doesn't work because you are running awk 937 times to count 937 directories, counting 937 separate "grand" totals.
Also: If your 'one liner' is three terminals wide, it probably shouldn't be a one-liner.
Also you can simplify it a lot. The 'echo' is pointless (and may actually be a bug). You can glob for 'statelist' instead of searching for it in every possible file and folder too. And you don't even need to cd -- or loop at all, for that matter -- to ls -la it.
ls -la */statelist | awk '!/^(total|d)/ && (NR>5) {SUM += $5} END { print "Total number of elements " SUM }'
1 Like
you use find -type d to insure that ${i} will only find directories
1 Like
That's a good idea, find will be simpler than ls here, but we want only files, not dirs...
find */statelist -type f -printf "%s\n" | awk '{ SUM+=$1 } END { print "sum " SUM }'
Where %s, to find, means 'size in bytes'. So we don't need to do any filtering in awk at all.