-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-largest
executable file
·99 lines (80 loc) · 2.51 KB
/
file-largest
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
#!/usr/bin/python3
import os
import sys
def kmg(bytes,kilo=1000, append='',thresh=15,rstrip0=1, extradigits=0):
""" 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
# Yes, can be abstracted now
if abs(bytes)>(0.80*peta):
showval = bytes/float(peta)
if showval<thresh:
showdigits = 1 + extradigits
else:
showdigits = 0 + extradigits
append+='P'
elif abs(bytes)>(0.80*tera):
showval = bytes/float(tera)
if showval<thresh:
showdigits = 1 + extradigits
else:
showdigits = 0 + extradigits
append+='T'
elif abs(bytes)>(0.95*giga):
showval = bytes/float(giga)
if showval<thresh: # e.g. 1.3GB but 15GB
showdigits = 1 + extradigits
else:
showdigits = 0 + extradigits
append+='G'
elif abs(bytes)>(0.9*mega):
showval = bytes/float(mega)
if showval<thresh:
showdigits = 1 + extradigits
else:
showdigits = 0 + extradigits
append+='M'
elif abs(bytes)>(0.85*kilo):
showval = bytes/float(kilo)
if showval<thresh:
showdigits = 1 + extradigits
else:
showdigits = 0 + extradigits
append+='K'
else:
showval = bytes
showdigits=0
ret = ("%%.%df"%(showdigits))%showval
if rstrip0:
if '.' in ret:
ret=ret.rstrip('0').rstrip('.')
ret+=append
return ret
data = [] # list of (size, filename)
print( "Reading...", end='')
cnt=0
for path in sys.argv[1:]:
for r,ds,fs in os.walk(path):
for fn in fs:
ffn = os.path.join( r, fn )
mode, inode, device, numhardlinks, uid,gid, size, atime, mtime, ctime = os.lstat(ffn)
data.append( (size, ffn) )
cnt += 1
if cnt%1000==0:
sys.stdout.write('%s '%cnt)
sys.stdout.flush()
print( "Done" )
# TODO: option parsing
show_amt = 30
for size, fn in sorted( data )[-show_amt:]:
print( '%6sB\t%r'%( kmg(size), fn ))