DEV Community

BC
BC

Posted on

How to show operation time when run `history` command - Linux Tips

We can use history command to get our history operations in console:

$ history | tail -5
  1659  vim test.sh
  1660  cat ping.log
  1661  ls
  1662  vim a.py
  1663  history | tail -5
  ...
Enter fullscreen mode Exit fullscreen mode

By default, history will only show a operation sequence number (the leading 1659, 1660, ... in this case) and the command name.

What if we want to see when we did such operation?

The trick is define HISTTIMEFORMAT environment variable:

$ export HISTTIMEFORMAT='%F %T '
Enter fullscreen mode Exit fullscreen mode

After define it, now let's run the history command again:

$ history | tail -5
  1661  2020-01-08 11:56:50 ls
  1662  2020-01-08 11:56:56 vim a.py
  1663  2020-01-08 11:57:02 history | tail -5
  1664  2020-01-08 11:57:41 export HISTTIMEFORMAT='%F %T '
  1665  2020-01-08 11:57:43 history | tail -5
  ...
Enter fullscreen mode Exit fullscreen mode

Now the time shows up.

But you may find commands that ran before we set the HISTTIMEFORMAT variable are showing the same time, for example:

$ history | head -10
  671  2020-01-08 11:56:25 kill -INT 16055
  672  2020-01-08 11:56:25 kill -INT 16094
  673  2020-01-08 11:56:25 kill -2 16122
  674  2020-01-08 11:56:25 kill -2 16138
  675  2020-01-08 11:56:25 kill -10 16162
  676  2020-01-08 11:56:25 ps axu | grep test.sh
  677  2020-01-08 11:56:25 kill -10 16174
  678  2020-01-08 11:56:25 kill -10
  679  2020-01-08 11:56:25 kill -10 16195
  680  2020-01-08 11:56:25 exit
Enter fullscreen mode Exit fullscreen mode

You can see in this case, all commands' time show as "2020-01-08 11:56:25", this is because before we set the HISTTIMEFORMAT variable, system didn't record the timestamp for each command. To show time for those old commands, system chose to use the earliest time it started to record.

In fact, the command histories are saved in ~/.bash_history file. If you check the content of it:

$ cat ~/.bash_history
...
ls
vim a.py
ls
exit
#1578501725
history | head -5
#1578501743
export HISTTIMEFORMAT='%F %T '
#1578501745
ls
#1578501748
history | head -5
#1578501829
history
#1578502154
exit
Enter fullscreen mode Exit fullscreen mode

You can see that after we set the HISTTIMEFORMAT variable, there is an extra timestamp like #1578501743 is saved associate with the command. When we run the history command, it will read this .bash_history file, convert the timestamp to human friendly format and show it with the command.

Top comments (3)

Collapse
 
ghost profile image
Ghost • Edited

I was just trying to remember when I did the last system update (to not overuse my distro servers), too bad I can only give this post 1 unicorn, I think your contribution will live in my .bashrc forever, that's a big compliment by the way, doesn't look that way but my bashrc is very old and wise.

Collapse
 
bitecode profile image
BC

🙌 Glad it helps, thank you Robert 😸

Collapse
 
ghost profile image
Ghost

how not to love Linux, we are often accused of being fanboys, but how can we not, I've used it for 13+ years and still keep finding this jewels, 1 line and you improve your system so much, no apps, no subscription, no premium version or spyware. A decade later and still fun as the first day.