Skip to content

Instantly share code, notes, and snippets.

@NISH1001
Created January 19, 2019 04:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NISH1001/bf2b713418b4e2ede8e6a7373b42c4c1 to your computer and use it in GitHub Desktop.
Save NISH1001/bf2b713418b4e2ede8e6a7373b42c4c1 to your computer and use it in GitHub Desktop.
log bash history to file

Vanilla Configuration

If you are not using any third party variants of bash like oh-my-bash, put the following command in your .bashrc:

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'

Close bashrc and open new terminal. Now, your history are saved in the folder ~/.logs according to timestamp.
Example file

/home/paradox/.logs/bash-history-2018-12-15.log
/home/paradox/.logs/bash-history-2018-12-16.log
/home/paradox/.logs/bash-history-2019-01-19.log

oh-my-bash

Now, if you are using oh-my bash, you have to put the above command in the function that creates prompt_command.
This is according to the type of theme you are using. The prompt command is generated in the function __powerline_prompt_command.
For me, I am using powerline theme. So, I have to put my logger command in powerline-base.sh file inside the function I have written above.

function __powerline_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
...
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment