Skip to content
Advertisement

How to keep 2 history files? One with timestamp, the other without

The reason I want to achieve this, is because I’d like to remove duplicates for easier command retrieval on the one hand, while still having the option of going over everything along with the timestamp, in case something bad happens, and perhaps going over what commands I executed can help somehow.

Here is what I currently have, which doesn’t work for erasing duplicates, because of the timestamp (but of course even if it employed a regexp to be able to delete duplicates with unique timestamps, I would lose the ability to see a list of all commands executed)

export HISTFILESIZE=100000000
export HISTSIZE=100000000
export HISTTIMEFORMAT="[%F %T] "
HISTCONTROL=ignoredups:erasedups
shopt -s histappend
PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND"
#http://unix.stackexchange.com/questions/18212/bash-history-ignoredups-and-erasedups-setting-conflict-with-common-history

Thanks!

Advertisement

Answer

I could think of a simple solution for this. Use bash utils to print the command output without the time-stamp like below:-

history | awk '{ s = ""; for (i = 4; i <= NF; i++) s = s $i " "; print s }' | uniq 

Note:- The history configuration $HISTTIMEFORMAT must be as specified in the question with the time-stamp for the awk command to work. It basically prints all the columns after the time-stamp and passes it to uniq which omits repeated lines. You could even just write ( > logfile) the contents to a file in $HOME.

You could also add the commands as a bash function and run it parallel with the original history command.

Add the following lines to ~/.bashrc file

function history_cool() {
   history | awk '{ s = ""; for (i = 4; i <= NF; i++) s = s $i " "; print s }' | uniq 
}

With this you can now run your commands history and history_cool in parallel

  $  history
  0  [2016-04-22 17:54:58] echo "foo"
  1  [2016-04-22 17:55:06] echo "bar"
  2  [2016-04-22 17:55:30] ls
  3  [2016-04-22 17:55:39] echo "foo"

  $  history_cool
  echo "foo"
  echo "bar"
  ls
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement