Cmdlog

List bash commands by frequency

#!/usr/bin/env python3
import os
history_file = os.path.expanduser("~/.bash_history")
counts = {}
with open(history_file, "r", errors="ignore") as f:
    for line in f:
        line = line.strip()
        if not line or line.startswith("#"):
            continue
        cmdline = line.split("|", 1)[0].strip()
        parts = cmdline.split()
        if not parts:
            continue
        cmd = parts[1] if parts[0] == "sudo" and len(parts) > 1 else parts[0]

        counts[cmd] = counts.get(cmd, 0) + 1
sorted_cmds = sorted(counts.items(), key=lambda x: x[1], reverse=True)
for cmd, n in sorted_cmds:
    print(f"{n} {cmd:20}")

Unknown's avatar

About Linuxgal

Need a spiritual home? Consider joining us at Mary Queen of the Universe Latter-day Buddhislamic Free Will Christian UFO Synagogue of Vishnu
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment