Created
July 1, 2026 05:37
-
-
Save krisk0/272111540370dc03df91243a23c0ecce to your computer and use it in GitHub Desktop.
Find files with zeroes inside, that occupy less space than they should
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
| #!/usr/bin/python3 | |
| import os, sys | |
| def find_sparse_files(start_path, top_n=50): | |
| results = [] | |
| for dirpath, _, filenames in os.walk(start_path): | |
| for f in filenames: | |
| fp = os.path.join(dirpath, f) | |
| if os.path.islink(fp): | |
| continue | |
| try: | |
| stat = os.stat(fp) | |
| apparent_size = stat.st_size | |
| # Linux allocates disk usage in st_blocks (512-byte increments) | |
| occupied_size = stat.st_blocks * 512 | |
| hole_size = apparent_size - occupied_size | |
| if hole_size > 0: | |
| results.append((hole_size, fp)) | |
| except OSError: | |
| continue # Skip unreadable files safely | |
| # Sort descending by hole size | |
| results.sort(key=lambda x: x[0], reverse=True) | |
| if results: | |
| # Print header | |
| print(f"{'Hole Size':<12} | {'File Path'}") | |
| print("-" * 60) | |
| for hole, path in results[:top_n]: | |
| print(f"{format_size(hole):<12} | {path}") | |
| else: | |
| print("No holes in files") | |
| def format_size(bytes_size): | |
| for unit in ['B', 'KB', 'MB', 'GB', 'TB', 'PB']: | |
| if bytes_size < 1024: | |
| return f"{bytes_size:.2f} {unit}" | |
| bytes_size /= 1024 | |
| return f"{bytes_size:.2f} EB" | |
| if __name__ == '__main__': | |
| target_dir = sys.argv[1] if len(sys.argv) > 1 else '.' | |
| print(f"Scanning '{target_dir}' for large unallocated spans...") | |
| find_sparse_files(target_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment