News for hackers. Stuff that matters.
Re:When did I run that command? Update your Bash prompt with... by countermeasure on Saturday April 18, 2020 @08:09PM (#22912233)
For years I wanted a Bash prompt which would show when a command actually ran.

I don't mean that it would show the time when the prompt was written to the terminal. That's easy.

I mean that it would show the time when the command entered at it was executed. Those two times can be very different.

This little hack updates your Bash prompt with the current time when you execute a command.

As someone who spends many hours every day in the terminal, it's improved my life. Maybe it can help you too.

Re:When did I run that command? Update your Bash prompt with... by nicwolff on Saturday April 18, 2020 @09:33PM (#22912716)
This is handy, but if you just want the ability to see when you ran a previous command you can add

    export HISTTIMEFORMAT="%d/%m/%y %T "
to your .bashrc and then

    history
will show the date and time before each command that was run.
Re:When did I run that command? Update your Bash prompt with... by kyuudou on Sunday April 19, 2020 @07:02AM (#22914963)
I'm sure most know this but if anyone thinks this is a way of doing forensics, keep in mind it can be easily skipped over by doing a non-standard logout, like "kill -9 $$" or if a remote ssh connection gets cut for whatever reason. The command history for a session won't get written until logout by default. A workaround such as forcing each entry to be written to a log file will have to be done to have better assurance of true history.
Re:When did I run that command? Update your Bash prompt with... by kubanczyk on Sunday April 19, 2020 @08:41AM (#22915514)
> forcing each entry to be written to a log file

When I last needed it ~2011, there was a small and simple tool called libsnoopy for that.

Emphasis on "log file" though, the "history file" is a different use case, fundamentally unfit for forensics.

Re:When did I run that command? Update your Bash prompt with... by blago on Sunday April 19, 2020 @12:03AM (#22913423)
I've had this for a while and I noticed at least two problems on my mac:

1) It made the history command run much, much slower (I have HISTSIZE=100000)

2) Dates tend to reset to a uniform but meaningless value. I think it's related to OS reboots.

Re:When did I run that command? Update your Bash prompt with... by countermeasure on Saturday April 18, 2020 @09:53PM (#22912801)
Excellent point. Seeing when a command in the history list was run is very useful.

My preference is just for a date in the history, rather than date and time. I find that when I'm searching history, date is usually granular enough for me, so I can omit the time and save a little bit of screen real estate.

Re:When did I run that command? Update your Bash prompt with... by ansible on Saturday April 18, 2020 @09:44PM (#22912767)
I had no idea! I was about to try out the linked article, but this is all I really need. Excellent!
Re:When did I run that command? Update your Bash prompt with... by countermeasure on Saturday April 18, 2020 @10:00PM (#22912832)
I recommend using both approaches together.

With an updating prompt, you can see when a command ran without having to type 'history'.

And adding a date/time stamp to history is very useful when you need to go back further - hours, days or months.

Re:When did I run that command? Update your Bash prompt with... by kortex on Saturday April 18, 2020 @09:14PM (#22912624)
Have you tried zsh with histdb? It's been an absolute gamechanger. Having a complete, precise, and effectively infinite history is like a second brain.

I know not everyone is on the zsh train, but it might be able to be ported to bash (it's just pre-hooks and some sqlite)

https://github.com/larkery/zsh-histdb

Re:When did I run that command? Update your Bash prompt with... by deadbunny on Sunday April 19, 2020 @02:05AM (#22913908)
A quick Google turns up this [1]. Haven't verified it works though.

1. https://github.com/thenewwazoo/bash-history-sqlite

Re:When did I run that command? Update your Bash prompt with... by kortex on Tuesday April 21, 2020 @12:01AM (#22931724)
Oh! I actually used this before switching to histdb. IIRC it works-ish but there was less polish than histdb.
Re:When did I run that command? Update your Bash prompt with... by countermeasure on Saturday April 18, 2020 @09:41PM (#22912751)
Bash is the only shell I've ever used, but one day when time permits I'm looking forward to taking zsh for a test drive.
Re:When did I run that command? Update your Bash prompt with... by tiny_epoch on Sunday April 19, 2020 @07:04PM (#22919536)
I've had the following in my ~/.bash_profile for quite some time. I leave it here in the hopes it help someone.

  ## History configuration, see HISTORY in bash(1)
  ## builtin fc, aka fix command, for history manipulation
  ## fc [-e ename] [-lnr] [first] [last]
  ##  -e or invoke editor ename; defaults to $FCEDIT
  ##  -l or list commands
  ##  -n or suppress entry numbers
  ##  -r or reverse command order
  ##  first, last selects a range of entries by numeric index
  ##
  ## fc -s [pat=rep] [cmd]
  ##  -s or substitute all occurences of pat with rep
  #
  #  - h() is short for history
  #  - r() is short for replay
  #  - always append to the history, don't overwrite
  #  - save multi-line commands in a single entry
  #  - insist on vim everywhere
  #  - don't record commands with a leading space or duplicates
  #  - constrain the size of the history file
  #  - ignore common things
  #  - constrain the number of history entries
  #  - set up nicer timestamps
  alias h="history"
  alias r="fc -s"
  shopt -s histappend
  shopt -s cmdhist
  export FCEDIT=$EDITOR
  export HISTCONTROL=ignoreboth
  export HISTFILE=$HOME/.bash_history
  export HISTFILESIZE=1048576
  export HISTIGNORE='ls:ls -l:ls -latr:ps -ef:fc:h:history:clear:exit'
  export HISTSIZE=8192
  export HISTTIMEFORMAT='[%F %T]  '
Re:When did I run that command? Update your Bash prompt with... by chrisweekly on Friday April 24, 2020 @01:02PM (#22970338)
Thanks, @tiny_epoch -- that was helpful. :)