Add sizegetter.py to calculate total size of files containing a specific keyword

This commit is contained in:
Tristan Smith 2024-04-05 22:49:10 -04:00
commit 95e2b086f3
3 changed files with 46 additions and 0 deletions

29
sizegetter.py Normal file
View file

@ -0,0 +1,29 @@
import re
import sys
def calculate_total_size(filename, keyword):
pattern = re.compile(rf'{re.escape(keyword)}.*?(\d+(?:\.\d+)?)\s(MiB|GiB)')
total_size_mib = 0
with open(filename, 'r', encoding='utf-8') as file:
for line in file:
match = pattern.search(line)
if match:
size, unit = match.groups()
size = float(size)
if unit == "GiB":
size *= 1024 # Convert GiB to MiB
total_size_mib += size
total_size_gib = total_size_mib / 1024
return total_size_mib, total_size_gib
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python script.py <path to file> <keyword>")
sys.exit(1)
filename = sys.argv[1]
keyword = sys.argv[2]
total_size_mib, total_size_gib = calculate_total_size(filename, keyword)
print(f"Total size of files containing '{keyword}': {total_size_gib:.2f} GiB ({total_size_mib:.0f} MiB)")

0
zipremove.py Normal file
View file

17
zipsize Normal file
View file

@ -0,0 +1,17 @@
#!/bin/bash
awk 'BEGIN{CONVFMT="%.2f"}
{
if ($1 ~ /^[0-9]+$/) {
byte=$1;
if (byte >= 1073741824)
{printf("%.2f GB\t%s\n", byte/1073741824, $4)}
else if (byte >= 1048576)
{printf("%.2f MB\t%s\n", byte/1048576, $4)}
else if (byte >= 1024)
{printf("%.2f KB\t%s\n", byte/1024, $4)}
else
{print $1 " B\t" $4}
}
else {print $0}
}'