Bash History Display Date And Time For Each Command

last updated in Categories , , , , , , , ,

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.


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, enter:
$ echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile

  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
$ 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 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


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


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at

Historical Comment Archive

40 comment

  1. Errant ‘ ” ‘; ought it to read:

    echo ‘export HISTTIMEFORMAT=”%d/%m/%y %T ‘ >> ~/.bash_profile



  2. Ha, ha – even I messed up ! I see the command has now been amended.

    echo ‘export HISTTIMEFORMAT=”%d/%m/%y %T ” ‘>> ~/.bash_profile


  3. End single quote is missing.
    Correct command is:
    echo ‘export HISTTIMEFORMAT=”%d/%m/%y %T “‘ >> ~/.bash_profile

    1. Can you please suggest how to comeout from bash to normal user. after giving this command not able to come out from bash

  4. @Deej / Abhijeet,

    Thanks for the heads up. Due to caching it takes a few seconds to minutes to clean out old html page cache. The faq has been updated.

  5. Thanks, this helps a lot in an enterprise domain, where different ppl run different commands. Nice trick;)

  6. This is bash specific feature. Refer to your local ksh(1) man page or see fc -l. But, there is no $HISTTIMEFORMAT for ksh.

  7. awesome tip. i’ve forgoten to pursue this, but i’ve always wanted to add that to my bash hist file


  8. Is there a way to have cumulative history file where commands typed by all users are logged with date and time?

  9. I don’t think this tip works for all version of Bash? Because I couldn’t get it working even I put this line in .bash_profile and .profile.

  10. @LA, you need to configure system accounting and auditing which can keep track of all user activities, system resource consumptions and more. History is personal user account specific tool. Also, user can delete history or ignore history file. Search our site for sar and auditing howtos.

    @cindy, refer to your local bash man page. You can find out bash version number with the following command:
    echo $BASH_VERSION

  11. Yes, I figured my bash is way too old to have this environment variable. The version that I have is “GNU bash, version 2.03.0(1)-release” and all the examples I found online is above 3.0. My collegue searched the “HISTTIMEFORMAT” in some man pages for history and other, we cannot find this variable. But it’s a nice tip to use. :(

  12. you can add it to /etc/profile so it is available to all users. I also add:

    # Make sure all terminals save history
    shopt -s histappend histreedit histverify
    shopt -s no_empty_cmd_completion # bash>=2.04 only

    # Whenever displaying the prompt, write the previous line to disk:
    PROMPT_COMMAND=’history -a’

    #Use GREP color features by default: This will highlight the matched words / regexes
    export GREP_OPTIONS=’–color=auto’
    export GREP_COLOR=’1;37;41′

  13. Good tip. We have multiple users connecting as root using ssh and running different commands. Is there a way to log the IP that command was run from?
    Thanks in advance.

    1. Just for anyone who might still find this thread (like I did today):

      export HISTTIMEFORMAT=”%F %T : $(echo $SSH_CONNECTION | cut -d\ -f1) : ”

      will give you the time format, plus the IP address culled from the ssh_connection environment variable (thanks for pointing that out, Cadrian, I never knew about that before), all right there in your history output.

      You could even add in $(whoami)@ right to get if you like (although if everyone’s logging in with the root account that’s not helpful).

  14. Yup, you can export one of this

    env | grep SSH
    SSH_CLIENT= 42387 22
    SSH_CONNECTION= 42387 22

    As their bash history filename

    set |grep -i hist

    So in profile you can so something like HISTFILE=/root/.bash_history_$(echo $SSH_CONNECTION| cut -d\ -f1)

  15. Hi
    Nice trick but unfortunately, the commands which were executed in the past few days also are carrying the current day’s (today’s) timestamp.

    Please advice.


    1. Hi Sohail,

      Yes indeed that will be the behavior of the system since you have just enabled on that day the HISTTIMEFORMAT feature. In other words, the system recall or record the commands which were inputted prior enabling of this feature. Hope this answers your concern.


      1. Hi Sohail,

        Yes, that will be the behavior of the system since you have just enabled on that day the HISTTIMEFORMAT feature. In other words, the system can’t recall or record the commands which were inputted prior enabling of this feature, thus it will just reflect on the printed output (upon execution of “history”) the current day and time. Hope this answers your concern.


  16. Hi

    The command only lists the current date (Today) even for those commands which were executed on earlier days.

    Any solutions ?


  17. hi u know any openings for freshers in linux field? I m doing rhce course from rajiv banergy. My samba,nfs-nis,dhcp,telnet,ftp,http,ssh,squid,cron,quota and system administration is over.iptables ,sendmail and dns is remaining.


  18. The command is not working properly. It is displaying the date and time of todays for all the commands where as I ran the some command three before.

    How come it is displaying the today date

  19. Hi..

    I want to collect the history of particular user everyday and want to send an email.I wrote below script.
    for collecting everyday history by time shall i edit .profile file of that user
    echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile

    #This script sends email of particular user
    history >/tmp/history
    if [ -s /tmp/history ]
           mailx -s "history 29042014"  </tmp/history
    rm /tmp/history

    Can any one suggest better way to collect particular user history for everyday

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

  20. 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.

  21. 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

    Still, have a question? Get help on our forum!