Skip to content

Instantly share code, notes, and snippets.

@srgrn
Last active September 4, 2022 17:48
Show Gist options
  • Save srgrn/51606d6f6aff950f7e8c7a5b2d0e2c70 to your computer and use it in GitHub Desktop.
Save srgrn/51606d6f6aff950f7e8c7a5b2d0e2c70 to your computer and use it in GitHub Desktop.
Save history to file based on date.
There is a whole discussion at https://news.ycombinator.com/item?id=20054082
however for bash i used
export PROMPT_COMMAND='if [ "$(id -u)" -ne 0 ]; then echo "$(date "+%Y-%m-%d.%H:%M:%S") $(pwd) $(history 1)" >> ~/.logs/bash-history-$(date "+%Y-%m-%d").log; fi'
i suspect it is from https://spin.atomicobject.com/2016/05/28/log-bash-history/
in the discussion there is reference to https://github.com/tkf/rash
another interesting option - https://www.thegeekdiary.com/how-to-log-every-shell-command-in-linux/
for zsh i used the precmd hook
by loading the following.
precmd() {
if [ "$(id -u)" -ne 0 ]
then echo "$(date "+%Y-%m-%d.%H:%M:%S") $(pwd) $(history -1)" >> ~/.logs/bash-history-$(date "+%Y-%m-%d").log
fi
}
it is possible that this would have to be changed to allow multiple precmd hooks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment