aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrahiel <rahielkasim@gmail.com>2014-11-17 23:22:02 +0100
committerrahiel <rahielkasim@gmail.com>2014-11-17 23:22:02 +0100
commitb2ef81f2c1e8287c8fdddbe1a0adc5833ec4db5d (patch)
treed8e9b2d0fa461deaccc92a0f01932ab69b262d83
parentfeb97536b82c559b91574ad3c02420f18923eddc (diff)
find shell history and display ten most used commands
-rw-r--r--shellstats.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/shellstats.py b/shellstats.py
new file mode 100644
index 0000000..9569b21
--- /dev/null
+++ b/shellstats.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from __future__ import print_function, division
+from os import getenv
+from os.path import isfile
+
+
+def main():
+ history_file = get_history()
+ if not history_file:
+ print("Shell history not found.")
+ with open(history_file, 'r') as h:
+ history = [l.strip() for l in h.readlines() if l.strip()]
+ commands = {}
+ for line in history:
+ cmd = line.split()
+ # if not cmd: continue
+ if cmd[0] in commands:
+ commands[cmd[0]] += 1
+ else:
+ commands[cmd[0]] = 1
+
+ total = len(history)
+ frequency = sorted(commands.keys(), key=lambda x: commands[x], reverse=True)
+ print("{:>3} {:<20} {:<10} {:<3}".format('', "Command", "Count", "Percentage"))
+ for i in range(10):
+ cmd = frequency[i]
+ count = commands[cmd]
+ print("{i:>3} {cmd:<20} {count:<10} {percent:<3.3}%"
+ .format(i=i+1, cmd=cmd, count=count, percent=count / total * 100))
+ return frequency
+
+
+def get_history():
+ """Get the history file for the shell in use."""
+ shell = getenv("SHELL").split('/')[-1]
+ home = getenv("HOME") + '/'
+ hist = {"bash": [".bash_history"], "fish": [".config/fish/fish_history"],
+ "zsh": [".zhistory", ".zsh_history"]}
+ if shell in hist:
+ for history in hist[shell]:
+ if isfile(home + history):
+ return home + history
+
+
+if __name__ == "__main__":
+ main()