Set Date and Time for Each Command You Execute in Bash History

By default, all commands executed by Bash on the command line are stored in history buffer or recorded in a file called ~/.bash_history. This means that a system administrator can view a list of commands executed by users on the system or a user can view his/her command history using the history command like so.

$ history
Linux History Command

Linux History Command

From the output of the history command above, the date and time when a command was executed is not shown. This is the default setting on most if not all Linux distributions.

In this article, we will explain how you can configure time stamp information when each command in the Bash history was executed to be displayed.

The date and time associated with each history entry can be written to the history file, marked with the history comment character by setting the HISTTIMEFORMAT variable.

There are two possible ways of doing this: one does it temporarily while the other makes it permanent.

To set HISTTIMEFORMAT variable temporarily, export it as below on the command line:

$ export HISTTIMEFORMAT='%F %T'

In the export command above, the time stamp format:

  1. %F – expands to full date same, as %Y-%m-%d (year-month-date).
  2. %T – expands to time; same as %H:%M:%S (hour:minute:seconds).

Read through the date command man page for additional usage information:

$ man date

Then check your command history as follows:

$ history 
Display Linux Command History with Date and Time

Display Linux Command History with Date and Time

However, if you want to configure this variable permanently, open the file ~/.bashrc with your favorite editor:

$ vi ~/.bashrc

And add the line below in it (you mark it with a comment as your own configuration):

#my config
export HISTTIMEFORMAT='%F %T'

Save the file and exit, afterwards, run the command below to effect the changes made to the file:

$ source ~/.bashrc

That’s all! Do share with us any interesting history command tips and tricks or your thoughts about this guide via the comment section below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Ravi Saive

I am Ravi Saive, creator of TecMint. A Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux. Follow Me: Twitter, Facebook and Google+

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

19 Responses

  1. Romualdo Soler González says:

    In my opinion, it’s a little clearer, like this: export HISTTIMEFORMAT=’%F %T ->

  2. Gaspar says:

    How to stop using history with date and time?

    which command i can use?

    • Ravi Saive says:

      @Gaspar,

      Remove the following line from your /.bashrc file to print date and time in history command.

      export HISTTIMEFORMAT='%F %T'
      
  3. Devendra says:

    I have done it but in history it showing all commands are executed on current date.

  4. monisha says:

    how to change date acoording to our choice

  5. Zaaker says:

    Can we set this if we don’t have root privilege on the server? I tried doing it by exporting as shown above .. it didn’t give me any error but it didn’t show the time and date in history

  6. mohammad says:

    Hello,
    Thanks million for your efforts and your great post.
    I want to show ip address (of the user that executed the command) in history command. How can i do that?

    Second question
    when i ssh to my linux machine with putty and suddenly my windows restart (for any reason) there is no history in next ssh
    but if i exit from putty with command “exit” in my server history will be saved for next ssh login.
    how can i set that each command to be saved even if putty closed abnormal?

  7. Alexey says:

    Thank you!

  8. Marek says:

    Modifying bash_history file won’t let you use other options, like re-running some command from history by using ! char at the beginning. IMHO, good, but not ideal

    • Nick says:

      Marek, I have this line: export HISTTIMEFORMAT=”%Y-%m-%d %T ”
      in my ~/.bash_profile and all works as expected. I can use !!

    • Aaron Kili says:

      @Marek

      You actually do not have to modify the bash_history file. This method simply adds the timestamp to a command entry in it, and you can re-run commands as usual.

  9. ThisHosting.Rocks says:

    Just don’t forget to update your actual system time to correlate with your IRL time. Usually the system’s (server) time is not the same as the one where you’re actually working from.

    It’s just easier, not that it really matters.

  10. Faissal says:

    Great!
    It’s nice to add a space or tab between the date and its command :)

    Thanks.

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.