Skip to content

Instantly share code, notes, and snippets.

@fsteffenhagen
Last active October 31, 2024 17:27
Show Gist options
  • Save fsteffenhagen/e09b827430956d7f1de35140111e14c4 to your computer and use it in GitHub Desktop.
Save fsteffenhagen/e09b827430956d7f1de35140111e14c4 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
@chunyang-wen
Copy link

on MacOS

brew install coreutils

Then enjoy numfmt

Man page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment