Bash tips : Adding time stamps to history command output



Here is a quick but useful tips that should help you lazy sysadmin !

What do you miss the most when investigating a failed system, or a disappeared directory ?

The issuing time for each command from the bash history.

Here is the solution (to this problem at least !)



1) Adding time to bash history command output

The simplest way to do that would be to add the following to your system bashrc (should be located somewhere in /etc for Red Hat based distros) :

export HISTTIMEFORMAT="%d.%m.%y. %T "


The corresponding one-liner :

echo 'export HISTTIMEFORMAT="%d.%m.%y. %T "' >> /etc/bashrc

Please note that using the above file (/etc/bashrc) for custom variable is not the best, as this file might be updated by system update etc.

You may, of course, use the date format that suits you best (see the date command for applicable output format).

2) Sample output

Before :

  991  cd Downloads/
  992  ll -tr
  993  ls
  994  ssh test
  995  timeCopy.bash 
  996  sudo yum update
  997  su -

After :

  991  13.08.14. 21:43:47 cd Downloads/
  992  13.08.14. 21:43:47 ll -tr
  993  13.08.14. 21:43:47 ls
  994  13.08.14. 21:43:47 ssh test
  995  13.08.14. 21:43:47 timeCopy.bash 
  996  13.08.14. 21:43:47 sudo yum update
  997  13.08.14. 21:43:47 su -



  • The blog from where I got this good tips, thanks to you


More “Sysadmin” posts

Tagged on: ,

Leave a Reply

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

This site supports SyntaxHighlighter via WP SyntaxHighlighter. It can highlight your code.
How to highlight your code: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag and format it when submitted.