Bash History Display Date And Time For Each Command

See all Bash/Shell scripting related FAQ
How do I display shell command history with date and time under a Linux, MacOS or Unix-like operating systems? How do I see date and time stamps in bash history command running on a Linux or Unix-like systems?

The bash shell provides access to the command history, the list of commands previously typed. The value of the HISTSIZE variable is used as the number of commands to save in a history list. If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file, marked with the history comment character. This page shows how to display bash History with date and time when running the history command on a Linux or Unix-like systems.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category Linux shell scripting
Prerequisites date command
OS compatibility BSD • Linux • macOS • Unix
Est. reading time 4 minutes

Bash History Display Date And Time For Each Command

By default history command will display output as follows:
$ history
Sample outputs:

 1994  ls S*.pdf
 1995  ls -lh S*.pdf
 1996  xdg-open Statement_Jun\ 2018.pdf 
 1997  xdg-open System_01Jun2018_30Jun2018_0133*.pdf
 1998  ssh vivek@ln.cbz01
 1999  gpass
 2000  history 

The above command won’t display what time the commands were executed from the bash history. To solve this problem create a shell variable called HISTTIMEFORMAT.

How to see time stamps in bash history

Defining the environment variable named HISTTIMEFORMAT as follows:
$ HISTTIMEFORMAT="%d/%m/%y %T "
OR add to your ~/.bash_profile file using the export command, enter:
$ echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
Where,

  1. %d – Day
  2. %m – Month
  3. %y – Year
  4. %T – Time

Use the source command to load HISTTIMEFORMAT from file into the current shell script or a command prompt:
$ . ~/.bash_profile
OR
$ source ~/.bash_profile

How to retrieve Linux or Unix bash command line history by date and time

Now, to see history, type:
$ history
Sample outputs:

....
..
  986  11/03/10 04:31:36 memcached-tool  10.10.28.22:11211 stats
  987  11/03/10 04:31:36 w
  988  11/03/10 04:31:37 iostat 
  989  11/03/10 04:31:37 top
  990  11/03/10 04:31:37 at
  991  11/03/10 04:31:38 atop
  992  11/03/10 04:31:40 collectl 
  993  11/03/10 04:31:41 grep CPU /proc/cpuinfo 
  994  11/03/10 04:31:45 vmstat 3 100
  995  11/03/10 04:31:55 sar -W -f /var/log/sa/sa12
....
..

Here is another output from my Ubuntu Linux server:

Fig.01: Adding date and time to my bash history i.e. Bash History Display Date And Time

Fig.01: Adding date and time to my bash history

Users set the HISTTIMEFORMAT variable. Bash uses its value to the format string to show the date/time stamp associated with each history entry displayed by the built-in history command. In other words, when this variable is set, time stamps are written to the history file so they may be preserved across shell sessions. This uses the history comment character to distinguish timestamps from other history lines.

Controlling Bash history behavior

We can use the shopt command to control optional shell behavior for the history command under Linux, macOS and Unix-like OS. The syntax is:

shopt -s {option} # set/enable 
shopt -u {option} # unset/disable
Table1: Fine tuning bash history options
Option Description Set value Unset value
histappend If set, the history list is appended to the file named by the value of the HISTFILE variable when the shell exits, rather than overwriting the file. set -s histappend

set -u histappend
histreedit If set, and readline is being used, a user is given the opportunity to re-edit a failed history substitution. set -s histreedit

set -u histreedit
histverify If set, and readline is being used, the results of history substitution are not immediately passed to the shell parser. Instead, the resulting line is loaded into the readline editing buffer, allowing further modification. set -s histverify

set -u histverify

Other shell variables

We can set the following additonal variables for history command:

Table 2: Controlling history file size and more
Environment variable
(use the export command)
Description
HISTCONTROL=ignoreboth:erasedups If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. Also ignoredups option causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups. We can set erasedups option that causes all previous lines matching the current line to be removed from the history list before that line is saved. Any value not in the above list is ignored.
PROMPT_COMMAND="history -a; history -n" Run history command before next prompt displayed. The -a option append history lines from this session to the history file. The -n read all history lines not already read from the history file and append them to the history list
HISTIGNORE="ls:ll:cd:pwd:bg:fg:history" A colon-separated list of patterns used to decide which command lines should be saved on the history list.
HISTSIZE=100000 The number of commands to remember in the command history.
HISTFILESIZE=10000000 The maximum number of lines contained in the history file.

Conclusion

You just learned how to display or see time stamps in bash history including date when the command were executed. For more info type the following commands or see gnu/bash page here:
$ man bash
$ help history
$ man 3 strftime

See “A Shell Primer: Master Your Linux, OS X, Unix Shell Environment” for more info.

This entry is 5 of 5 in the Bash HISTORY Tutorial series. Keep reading the rest of the series:
  1. How to disable bash shell history in Linux
  2. How to delete a single command from history on Linux/Unix Bash shell
  3. Clear Shell History In Ubuntu Linux
  4. Clear Linux / UNIX BASH Shell Command Line Cache / History
  5. Bash History: Display Date And Time For Each Command

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

49 comments… add one
  • Vanathu Oct 30, 2014 @ 1:01

    its show only current date for all the command history

    • lefty.crupps Oct 30, 2014 @ 2:08

      it’s marking all of your current history with today’s date. Try checking again in a few days.

  • tinu Oct 14, 2015 @ 15:30

    Hi All,

    I Have enabled my history with the command given :
    echo ‘export HISTTIMEFORMAT=”%d/%m/%y %T “‘ >> ~/.bash_profile

    i need to know how i can add the ip’s also , from which the commands are fired to the system.

  • bharadhi May 13, 2016 @ 10:36

    How to set the history with date format for cshell and also let me know why cshell users also having bashrc, bash profile, in its home path

  • Álvaro Jun 9, 2016 @ 9:41

    this shit does not works.

    • DivineOrator Apr 25, 2024 @ 18:03

      PICNIC: Problem In Chair Not In Computer.

  • Thalia Corbyn Apr 15, 2021 @ 18:50

    Worked like a charm on my new Macbook pro xD. I hate zfs. I switched back to bash and this all I was missing.

  • Vera Sutton Apr 16, 2021 @ 3:29

    My PROMPT_COMMAND to keep existing PROMPT_COMMAND settings

     export PROMPT_COMMAND="history -a;history -r;$PROMPT_COMMAND"
    
  • Page Ingram May 3, 2021 @ 6:21

    From my ~/.bash_aliases file:

    shopt -s histappend
    shopt -s histappend histreedit histverify
    shopt -s no_empty_cmd_completion
    export HISTCONTROL=ignoreboth:erasedups
    export PROMPT_COMMAND="history -a; history -n"
    export HISTIGNORE="ls:ll:cd:pwd:bg:fg:history"
    export HISTSIZE=100000
    export HISTFILESIZE=10000000
    export HISTTIMEFORMAT="%d/%m/%y %T "
    

    It is so nice to have clean and easy to read history.

    • Anton Jun 5, 2021 @ 15:09

      I’m confused aren’t these supposed to be in your .bash_profile or .profile. So i can put all of these settings in my .bash_aliases?

      • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Jun 5, 2021 @ 21:05

        On many Linux distros, there is a separate file called ~/.bash_aliases. Unfortunately, it is not standard across all Unix variants. It is best to stick with ~/.bash_profile or ~/.profile file to avoid confusion. YMMV.

  • Francisco Mancardi Aug 29, 2022 @ 6:30

    Thanks a lot for the info

  • Stephen Twombly Dec 13, 2022 @ 15:43

    Just remember the commands run BEFORE enabling this will all show the same timestamp, so you cannot back-date those. This must be enabled before you need it

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.