Skip to content

Instantly share code, notes, and snippets.

@killerbees19
Forked from fsteffenhagen/sum_filesize.sh
Created January 16, 2023 07:23
Show Gist options
  • Save killerbees19/a64b7213c34aec423ef374cea305f51f to your computer and use it in GitHub Desktop.
Save killerbees19/a64b7213c34aec423ef374cea305f51f to your computer and use it in GitHub Desktop.
sum human readable file sizes with numfmt and awk
# Input: list of rows with format: "<filesize> filename", e.g.
# filesizes.txt
#######################
# 1000K file1.txt
# 200M file2.txt
# 2G file3.txt
#
# Output:
cat filesizes.txt | numfmt --from=iec | awk 'BEGIN {sum=0} {sum=sum+$1} END {printf "%.0f\n", sum}'
2358222848
cat filesizes.txt | numfmt --from=iec | awk 'BEGIN {sum=0} {sum=sum+$1} END {printf "%.0f\n", sum}' | numfmt --to=iec
2.2G
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment