-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile-totalsize
More file actions
executable file
·114 lines (94 loc) · 2.99 KB
/
file-totalsize
File metadata and controls
executable file
·114 lines (94 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python3
'''
Takes a list of filenames, sums their sizes
Meant as a tool to stick on the end of find and grep
'''
import sys, os, optparse
def kmg(bytes,kilo=1000, append='',thresh=15,rstrip0=1):
""" Readable size formatter
e.g. '%sB'%kmg(2342342324) = '2.3 GB'
kmg(3429873278462) == '3.1T'
kmg(342987327) == '327M'
kmg(34298) == '33K'
Decimal/SI kilos by default. Specify kilo=1024 if you want binary kilos.
Maybe use sfloat-like behaviour for the number?
"""
ret=None
mega=kilo*kilo
giga=mega*kilo
tera=giga*kilo
peta=tera*kilo
if abs(bytes)>(0.80*peta):
showval = bytes/float(peta)
if showval<thresh:
ret= "%.1f"%(showval,)
else:
ret= "%.0f"%(showval,)
append+='P'
elif abs(bytes)>(0.80*tera):
showval = bytes/float(tera)
if showval<thresh:
ret= "%.1f"%(showval,)
else:
ret= "%.0f"%(showval,)
append+='T'
elif abs(bytes)>(0.95*giga):
showval = bytes/float(giga)
if showval<thresh: # e.g. 1.3GB but 15GB
ret= "%.1f"%(showval,)
else:
ret= "%.0f"%(showval,)
append+='G'
elif abs(bytes)>(0.9*mega):
showval = bytes/float(mega)
if showval<thresh:
ret= "%.1f"%(showval,)
else:
ret= "%.0f"%(showval,)
append+='M'
elif abs(bytes)>(0.85*kilo):
showval = bytes/float(kilo)
if showval<thresh:
ret= "%.1f"%(showval,)
else:
ret= "%.0f"%(showval,)
append+='K'
else:
ret= "%d"%(bytes,)
if rstrip0:
if '.' in ret:
ret=ret.rstrip('0').rstrip('.')
ret+=append
return ret
if __name__ == '__main__':
p=optparse.OptionParser()
p.add_option('-0','--null', default=False, action="store_true", dest='null',
help='Assume input is 0x00-delimited rather than whitespace-delimited')
options,args = p.parse_args()
indata = sys.stdin.read()
if options.null:
inlist = indata.split('\x00')
else:
inlist = indata.split()
errors = 0
successes = 0
size = 0
for infile in inlist:
if infile=='':
continue
try:
stob = os.stat(infile)
size += stob.st_size
successes += 1
except Exception as e:
# maybe separate out 'not found' from others?
#print 'ERROR',e,repr(infile)
errors += 1
print( "%siB / %sB (%d bytes) in %d files"%( kmg(size, kilo=1024),
kmg(size, kilo=1000),
size,
successes), end='')
if errors>0:
print( " (%d access errors)"%errors)
else:
print( "")