Search code examples
zshalias

Most frequently used commands during the last x months


I know how to get the most used shell commands in zsh with

history 1 | awk '{$1="";print substr($0,2)}' | sort | uniq -c | sort -n | tail -n 20

but is there a way to restrict myself to let's say the last two or three months?

I need this because I would like to create aliases for the commands I am currently using most.


Solution

  • history in zsh have several flags to show date and time stamp. For this to work, you have to add setopt extended_history to your .zshrc file.

    If you have extended_history enabled, history -i will show full time-date stamps in ISO8601 `yyyy-mm-dd hh:mm' format. Dates in this format can be compared as strings. So just change your awk script and use it to select only lines after some date.

    history -i 1 | awk '{ if ($2 >= "2020-05-01") { $1=$2=$3="";print $0; } }'  | sort | uniq -c | sort -n -r | head -n 20
    

    Be aware that if you have HIST_IGNORE_ALL_DUPS or HIST_IGNORE_DUPS options enabled, this will not work as intended.

    You can also use date command to get older date automatically.