Monday, 4 November 2013

Using history

I tend to forget commands, especially when they get longer or more complex. That's when "history" gets handy. You might want to search in history,
$ history | grep shutdown
1011  shutdown -h
1012  sudo shutdown -h now
This will give you all shutdown commands from your history. You can also check command history, without running it.
$ !yourcommand:p
Or if you need to execute multiple commands from history.
$ !219 ; !229 ; !221
The last example is how to list your most used commands.

$ history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head