Skip to content

Instantly share code, notes, and snippets.

@fsteffenhagen
Last active June 16, 2026 12:30
Show Gist options
  • Select an option

  • Save fsteffenhagen/e09b827430956d7f1de35140111e14c4 to your computer and use it in GitHub Desktop.

Select an option

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
@iamh2o

iamh2o commented Jul 16, 2021

Copy link
Copy Markdown

Super helpful! Thanks.

@jkern888

jkern888 commented Mar 18, 2022

Copy link
Copy Markdown

This is awesome. Thanks for sharing it!

@chunyang-wen

Copy link
Copy Markdown

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