Posts Command history with timestamp in Linux/Ubuntu
Post
Cancel

Command history with timestamp in Linux/Ubuntu

Display history with timestamp in Linux/Ubuntu

By default, the history command will display output as follows:

1
2
3
4
5
1  cd
2  touch abc.text
3  mv abc.text def.text
4  rm def.text
5  history

How to display timestamps of history in Bash

Define the environment variable named HISTTIMEFORMAT:

HISTTIMEFORMAT="%d/%m/%y %T "

  • %d: Day
  • %m: Month
  • %y: Year
  • %T: Time

Example:

1
2
HISTTIMEFORMAT="%d/%m/%y %T "
history

Sample output:

1
2
3
4
5
1  26/08/21 11:02:32 cd
2  26/08/21 11:02:32 touch abc.text
3  26/08/21 11:02:32 mv abc.text def.text
4  26/08/21 11:02:32 rm def.text
5  26/08/21 11:02:32 history

Or you can add directly to your ~/.bash_profile file:

1
2
3
echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
source ~/.bash_profile
history

How to display timestamps of history in Zsh

In zsh, it’s so easier with built-in commands:

Source: https://github.com/ohmyzsh/ohmyzsh/blob/12669f29f0843b8b980dd137f150a74511f88842/lib/history.zsh

1
2
3
4
5
6
7
case ${HIST_STAMPS-} in
  "mm/dd/yyyy") alias history='omz_history -f' ;;
  "dd.mm.yyyy") alias history='omz_history -E' ;;
  "yyyy-mm-dd") alias history='omz_history -i' ;;
  "") alias history='omz_history' ;;
  *) alias history="omz_history -t '$HIST_STAMPS'" ;;
esac

Let’s try these commands:

1
2
3
4
5
6
7
8
9
10
11
history -f
# 1  8/26/2021 11:02 cd
# 2  8/26/2021 11:02 touch abc.text

history -E
# 1  26.8.2021 11:02 cd
# 2  26.8.2021 11:02 touch abc.text

history -i
# 1  2021-08-26 11:02 cd
# 2  2021-08-26 11:02 touch abc.text

“The earlier you start working on something, the earlier you will see results.”

This post is licensed under CC BY 4.0 by the author.