Add my cdir script
- ID
d720cf0- date
2022-03-19 09:30:41+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
b97a6a9- message
Add my cdir script- changed files
1 file, 52 additions
Changed files
cdir (0) → cdir (1345)
diff --git a/cdir b/cdir
new file mode 100755
index 0000000..17c8b6b
--- /dev/null
+++ b/cdir
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+"""
+This script prints a short table of the subdirectories with the most entries,
+plus the total number of entries in a directory. It's useful if I'm trying
+to clean up a disk, and I'm looking for directories where I can make quick
+and easy gains.
+
+ 37 fishconfig
+ 48 repros
+ 51 colossus-wheels
+ 70 services
+ 292 .git
+ ===== =========================
+ 699
+
+It's also useful for finding the directory that's making backup software
+unhappy because there's lots of filesystem activity (e.g. `node_modules`).
+
+I often use this in conjunction with DaisyDisk (https://daisydiskapp.com/),
+which breaks down directories by size.
+
+"""
+
+import collections
+import os
+
+
+def count_entries_under(d):
+ assert os.path.isdir(d)
+ total = 1
+
+ for _, dirnames, filenames in os.walk(d):
+ total += len(dirnames)
+ total += len(filenames)
+
+ return total
+
+
+if __name__ == "__main__":
+ prefixes = collections.Counter()
+
+ for e in os.listdir("."):
+ if os.path.isfile(e):
+ prefixes["."] += 1
+ else:
+ prefixes[e] = count_entries_under(e)
+
+ for prefix, count in reversed(prefixes.most_common()):
+ print("%5d\t%s" % (count, prefix))
+
+ print("=====\t%s" % ("=" * max(len(p) for p in prefixes)))
+ print("%5d" % sum(prefixes.values()))
\ No newline at end of file