Is there a way to persist the command history and search it or group it by command?
In other words, I would like to list, say every grep command I have done over the last 3 months. Is that capability possible somehow?
Assuming you are using bash which is the most common shell
will show you the history that is stored in your .bash_history file. You can increase the size of and add dates in the history by adding
HISTSIZE=1000 HISTTIMEFORMAT="%Y/%m/%d %T "
To your .bashrc file. Then you can
history | grep " 2019/0[1-3]"
Note that bash only keeps history for a selection of commands in interactive sessions. If you want to track everything a custom shell wrapper, remote logging, and versioning the whole system are advisable.
To have every command done I would not count on the bash history for several reasons. You can send such commands to the syslog service/daemon.
I used to have a backlog of 1+ year command logs performed in 200+ Debian servers, in a secure central syslog server.
I used a simple "hack" of adding in
/etc/bash.bashrc for all users or. bash_profile just for your user the following line:
readonly PROMPT_COMMAND='history -a >(logger -t "cmdline $USER[$PWD] $SSH_TTY $SSH_CONNECTION")'
see related Add BASHPID to history?