Bash History: Display Date And Time For Each Command

by on March 11, 2010 · 31 comments· LAST UPDATED March 11, 2010


How do I display shell command history with date and time under UNIX or Linux operating systems?

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. Defining the environment variable as follows:
$ HISTTIMEFORMAT="%d/%m/%y %T "
$ echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
%d - Day
%m - Month
%y - Year
%T - Time
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


For more info type the following commands:
man bash
help history
man 3 strftime

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 31 comments… read them below or add one }

1 Deej March 11, 2010 at 11:36 am

Errant ‘ ” ‘; ought it to read:

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




2 Deej March 11, 2010 at 11:37 am

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

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



3 Abhijeet Vaidya March 11, 2010 at 11:41 am

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


4 Fouzan March 14, 2012 at 12:47 pm

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


5 nixCraft March 11, 2010 at 11:46 am

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


6 Somaikeres March 11, 2010 at 12:17 pm

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


7 Rahul Kolan March 11, 2010 at 1:25 pm

how to set this on solaris 10 and how to refresh local.login in solaris …


8 nixCraft March 11, 2010 at 1:34 pm

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


9 jake March 11, 2010 at 1:56 pm

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



10 LA March 11, 2010 at 2:39 pm

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


11 cindy March 11, 2010 at 2:51 pm

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.


12 nixCraft March 11, 2010 at 3:03 pm

@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:


13 cindy March 11, 2010 at 4:06 pm

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. :(


14 izaak March 12, 2010 at 11:06 am

I would also add
$ echo 'export HISTSIZE=10000' >> ~/.bash_profile

It’s really useful, I think.


15 Dariusz March 12, 2010 at 2:31 pm

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′


16 Babar Haq March 15, 2010 at 6:25 am

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.


17 cadrian March 16, 2010 at 5:55 pm

Yup, you can export one of this

env | grep SSH
SSH_CLIENT= 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)


18 TSI March 21, 2010 at 10:29 am

bash 4 can syslog every command bat afaik, you have to recompile it (check file config-top.h). See the news file of bash:
If you want to safely export history of your luser, you can ssl-syslog them to a central syslog server.


19 Dinesh Jadhav November 12, 2010 at 11:00 am

This is good command, It helps me a lot.


20 Indie September 19, 2011 at 11:41 am

You only need to use


in your .bash_profile


21 lalit jain October 3, 2011 at 9:58 am

— show history with date & time



22 Sohail January 13, 2012 at 7:05 am

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.



23 Raymond March 15, 2012 at 9:05 am

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.



24 Raymond March 15, 2012 at 9:08 am

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.



25 Sohail February 24, 2012 at 6:45 am


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

Any solutions ?



26 nitiratna nikalje August 24, 2012 at 5:24 pm

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.



27 JMathew August 26, 2012 at 10:51 pm


Is there anyway to log username also along with the Command Which we typed …

Thanks in Advance


28 suresh May 22, 2013 at 1:42 pm

How can i get full comman along with data and path as we het in history command.


29 rajesh December 6, 2013 at 5:56 am

Thanks… it worked..


30 Krishan February 7, 2014 at 6:18 am

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


31 PR April 29, 2014 at 5:18 pm


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


Leave a Comment

Tagged as: , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: