Created
May 29, 2011 18:25
-
-
Save sillygwailo/998012 to your computer and use it in GitHub Desktop.
List the most-used commands from your Bash history file
This file contains 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/env python | |
import operator, re, os.path | |
# http://stackoverflow.com/questions/613183/python-sort-a-dictionary-by-value | |
# http://stackoverflow.com/questions/4510709/python-find-index-of-first-digit-in-string | |
# http://docs.python.org/tutorial/inputoutput.html | |
# TODO: | |
# - account for commands piped in | |
# - account for sudo | |
# - switch to sort accending | |
# - argument to look at another user's top commands instead of just the current user | |
def top_commands(): | |
commands = {} | |
f = open(os.path.expanduser('~') + '/.bash_history') | |
for line in f.readlines(): | |
m = re.search(' ', line) | |
if m != None: | |
command = line[:m.start()] | |
if command not in commands: | |
commands[command] = 1 | |
else: | |
commands[command] += 1 | |
sorted_commands = sorted(commands.iteritems(), key=operator.itemgetter(1), reverse=True) | |
return sorted_commands | |
if __name__ == '__main__': | |
for top_command in top_commands(): | |
print top_command[0], top_command[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment