Last active
December 3, 2025 12:12
-
-
Save taikedz/66f4a423b71665e8e9354f223ffd8fb9 to your computer and use it in GitHub Desktop.
Sum of `du -sh` sizes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| du -sh * | python3 sum.py | |
| """ | |
| import sys | |
| import re | |
| numpat = re.compile("(^[0-9.]+)([a-zA-Z]*)") | |
| total = 0.0 | |
| for line in sys.stdin: | |
| tokens = line.strip().split() | |
| if tokens: | |
| m = re.match(numpat, tokens[0]) | |
| if not m: | |
| print(f"-- Skipped {tokens}") | |
| continue | |
| val = float(m.group(1)) | |
| fac = m.group(2).lower() | |
| if fac == "": | |
| fac = 1 | |
| elif fac == "k": | |
| fac = 1024 | |
| elif fac == "m": | |
| fac = 1024**2 | |
| elif fac == "g": | |
| fac = 1024**3 | |
| total += fac*val | |
| if total < 1024: | |
| print(f"-> {total}") | |
| elif total < 1024 ** 2: | |
| print(f"-> {total/1024:.3f} K") | |
| elif total < 1024 ** 3: | |
| print(f"-> {total/(1024**2):.3f} M") | |
| else: | |
| print(f"-> {total/(1024**3):.3f} G") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment