This would be fantastic if all it had was the instructions to see your own top commands. What a great way to discover opportunities for automation and time saving!
Be careful if you are in the habit of using environment variables to specify API keys or database passwords. One of my top commands is `FACEBOOK_SECRET=...`.
Excellent point. I'm definitely going to put that caveat at the beginning of any analysis I post. (and thanks to ctrl_freak for posting an alternative!)
It's ironic that the standard one-liner for this uses a pipeline but only counts the first word of the first command of the pipeline. Given itself as input, "sort" ought to be the most used command, but "history" is the only one counted. (I bring this up not for the sake of standard HN nitpicking but to point out that you probably do want to cover at least `sort` in your workshop, and it will be underrepresented in these results, along with `grep`, `wc`, etc.)
Unfortunately, shell grammar is complex enough that a correct one-liner is probably infeasible. For example, one of my top "commands" if you count by words is an environment variable setting prepended to an actual command.
EDIT: you can get good enough results by just splitting on "|", as others have suggested here -- any parts of regular expressions, etc that aren't really commands will probably be infrequent enough to get lost in the noise, and treating || as containing an empty command won't hurt. If you're going to catch ||, though, might as well get && too... and now you're going down the rabbit hole :-)
Quick hack - you can include the commands you pipe to with this:
history \
| sed "s/^[0-9 ]*//" \
| sed "s/ *| */\n/g \
| awk '{print $1}' \
| sort \
| uniq -c \
| sort -rn \
| head -n 100 \
> commands.txt
I haven't tried to account for pipe symbols inside strings - it didn't seem work it.
In case there are commands you want then to exclude (which I do) then you might want to "head -200", remove the commands you don't want to provide, and then trim to 100.
Added in edit - having done this a good half of my top 100 are actually scripts, so this is pretty pointless for me unless I rummage through them to find the common commands.
Added in edit again:
OK, here's a version that only includes actual system commands, and hence filters out all my personal scripts and commands:
history \
| sed "s/^[0-9 ]*//" \
| sed "s/ *| */\n/g" \
| awk '{print $1}' \
| xargs which \
| sed "s.^/usr.." \
| grep ^.bin \
| sed "s/^.*\///" \
| sort \
| uniq -c \
| sort -rn \
> commands.txt
I think you want s///g on your second sed... Also that doesn't work in Mac OS X for some reason (their sed doesn't appear to interpret \n in the replacement text). I replaced it with perl to make that part work:
perl -pe 's/ *\| */\n/g'
I still haven't gotten the whole thing to work yet because my history contains the above history pipeline and so it's splitting the "|" that inside the sed command onto multiple lines which is causing "xargs which" to balk because quotes are not matching or something:
xargs: unterminated quote
Shells are amazing until spaces or quotes are involved! :-)
I run wildly different commands on different hosts so I actually took three hosts I'm often logged into and combined their most run commands in history into one 100 line file.
Super-facile gpm plopping grabs across six nox tty consoles onto emacs and as apps arguments is the most pleasant, productive, and relaxed user interface I know.
738 ll (alias for a more reformed `ls` output)
549 python
457 cd
397 git
173 exit
157 vim
150 less
142 ssh
122 ./synchVeiled.sh ( script I'd been using a lot for a project )
121 sudo
109 scp
109 fab
101 gca ( alias for "git commit -am" )
97 curl
88 bash
74 grep
68 startx
56 rm
56 lguf ( alias to show all files in a git repo that are not being tracked )
55 history
54 whois
49 htop
48 source
40 echo
39 foreman
My history file is de-duplicated (oh-my-zsh enables hist_ignore_dups), so this sort of approach is never accurate.
Git comes out on top, but presumably because most of my invocations are unique ("git commit -m ..."), while things like "ls" are farther down the list.
It might just be my OCD showing, but 'clear' is my number one by an order of magnitude.
next 20:
177 ls
167 cd
122 brew
120 vim
119 cabal
117 cindy (an alias for ssh'ing to my local dev box)
115 sudo
114 tmux
111 man
110 less
110 openssl
109 rm
108 whois
107 ghci
107 lein
106 ssh
86 strings
75 smbclient
75 racket
65 nc
80 comments
[ 0.33 ms ] story [ 224 ms ] threadI can't wait for the full results.
I'll also post the results to HN, of course :)
`history -100`
on OS X, anyway, the default only gives 15 or so commands in history
I use the awkscript below to compact it:
# histsort.awk --- compact a shell history file # Thanks to Byron Rakitzis for the general idea
Isn't it safer to create a credentials file and give it the appropriate chmod?
http://www.12factor.net/config
112 6725 0.1 36.7 1965924 1411164 ? SLl May03 37:11 /usr/lib/jvm/java-7-openjdk-amd64//bin/java -Xms1g -Xmx1g -Xss256k -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -Delasticsearch -Des.pidfile=/var/run/elasticsearch.pid -Des.path.home=/usr/share/elasticsearch -cp :/usr/share/elasticsearch/lib/elasticsearch-0.90.0.jar:/usr/share/elasticsearch/lib/:/usr/share/elasticsearch/lib/sigar/ -Des.default.config=/etc/elasticsearch/elasticsearch.yml -Des.default.path.home=/usr/share/elasticsearch -Des.default.path.logs=/var/log/elasticsearch -Des.default.path.data=/var/lib/elasticsearch -Des.default.path.work=/tmp/elasticsearch -Des.default.path.conf=/etc/elasticsearch org.elasticsearch.bootstrap.ElasticSearch
Edit:
works for me.Unfortunately, shell grammar is complex enough that a correct one-liner is probably infeasible. For example, one of my top "commands" if you count by words is an environment variable setting prepended to an actual command.
EDIT: you can get good enough results by just splitting on "|", as others have suggested here -- any parts of regular expressions, etc that aren't really commands will probably be infrequent enough to get lost in the noise, and treating || as containing an empty command won't hurt. If you're going to catch ||, though, might as well get && too... and now you're going down the rabbit hole :-)
In case there are commands you want then to exclude (which I do) then you might want to "head -200", remove the commands you don't want to provide, and then trim to 100.
Added in edit - having done this a good half of my top 100 are actually scripts, so this is pretty pointless for me unless I rummage through them to find the common commands.
Added in edit again:
OK, here's a version that only includes actual system commands, and hence filters out all my personal scripts and commands:
Some systems seem to require \r instead of \n - I know vim's behavior differs from sed's in this, so that might be an issue.
With xargs balking, you can throw the error stream at that point, for convert it to a loop over the alleged commands:
Specifically: And yes spaces are a pig, and can lead to all sorts of ambiguities that don't have reasonable ways of resolving them, especially in filenames.Take the poster's script above and copy it to a text file. Now replace \n with a new line, save and it should run.
history \ | sed "s/^[0-9 ]//" \ | perl -pe 's/ \| */\n/g' \ | gawk '{counts[$1] += 1} END { for (x in counts) { print counts[x],x}}' OFS="\t" \ | column -t \ | sort -k 1,1nr \ | head -100
Ok not sure how to format this on HN https://gist.github.com/nyxwulf/5608955#file-gistfile1-sh
They work on teaching scientists to be better at software, and are always looking for people to do workshops
I suppose you could guess that my Linux box is an LNMP server pretty easily.
Fun project, good luck!
Git comes out on top, but presumably because most of my invocations are unique ("git commit -m ..."), while things like "ls" are farther down the list.
Relevant: https://github.com/paulmars/huffshell
I guess the future is all about regular expressions :)
next 20: