Created
June 20, 2016 13:41
-
-
Save LogIN-/3a976a1e210ff5c6044d4bca6059ad76 to your computer and use it in GitHub Desktop.
Analyze disk space usage of some common hidden/auto-generated files
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
| import fnmatch, os | |
| def bytesto(bytes, to, bsize=1024): | |
| """convert bytes to megabytes, etc. | |
| sample code: | |
| print('mb= ' + str(bytesto(314575262000000, 'm'))) | |
| sample output: | |
| mb= 300002347.946 | |
| """ | |
| a = {'k' : 1, 'm': 2, 'g' : 3, 't' : 4, 'p' : 5, 'e' : 6 } | |
| r = float(bytes) | |
| for i in range(a[to]): | |
| r = r / bsize | |
| return(r) | |
| def get_size(start_path = '/tank/XXXXXX'): | |
| txt_file = "/tmp/filelist.csv" | |
| # Empty file | |
| open(txt_file, 'w').close() | |
| fh = open(txt_file,"w") | |
| fh.write("FilePath\tFileSize\n") | |
| total_size = 0 | |
| file_names = ['Thumbs.db', '._.DS_Store', '.DS_Store', '._AppleDouble', '.apdisk', 'Desktop.ini', '.VolumeIcon.icns', '.TemporaryItems', '.Trashes', '._.TemporaryItems'] | |
| #file_names_regex = '*._*' | |
| skip_dirs = ['.zfs'] | |
| for dirpath, dirnames, filenames in os.walk(os.path.abspath(start_path), followlinks=False): | |
| if '.zfs' in dirnames: | |
| dirnames.remove('.zfs') | |
| continue | |
| if not dirnames and not filenames: | |
| fh.write(dirpath + "\t" + str(0) + "\n") | |
| for f in filenames: | |
| fp = os.path.join(dirpath, f) | |
| # Skip Symlinks | |
| if os.path.islink(fp): | |
| continue | |
| file_size = bytesto(os.path.getsize(fp), 'm') | |
| if file_size > 5000: | |
| fh.write(fp + "\t" + str(file_size) + "\n") | |
| if any(f in s for s in file_names): | |
| total_size += file_size | |
| fh.write(fp + "\t" + str(file_size) + "\n") | |
| # os.remove("/tmp/foo.txt") | |
| fh.close() | |
| return total_size | |
| size = get_size() | |
| print str(size) + " MB" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment