forked from SimonRundstedt/vhelpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsizeofdeleted
executable file
·36 lines (29 loc) · 1.12 KB
/
sizeofdeleted
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
#!/usr/bin/env python3
'''
List size of unlinked but not yet deleted files in bytes.
Recommended to run as root.
'''
import subprocess
import sys
CMD = "lsof | grep -i '(deleted)'" # Get all deleted files.
def sizeofdeletedfiles():
'''
Returns the size of deleted but not removed file or lsof return code if
something went wrong.
'''
res = subprocess.run(CMD,capture_output=True,shell=True)
if res.returncode:
return res.returncode
resrows = res.stdout.decode('utf-8').split("\n")
fsize = (int(r.split()[-3]) for r in resrows if r) # Negative index since
# not all row-column
# cells are populated in
# col 3 and 4
return sum(fsize)
if __name__ == "__main__":
res = sizeofdeletedfiles()
if res < 256: # Assume something went wrong.
sys.stderr.write(f"Warning: Result of {res} is less than 255. This is "\
f"likely an error returncode from lsof.")
sys.exit(1)
print(res)