Linux history – add time and date stamp to output

9:50 PM in Programming, System Administration by Vic Russell

The Linux ‘history‘ command is very beneficial when trying to debug a system, remember a previously executed command, or keep track of recent steps taken in a multi-command process.  One omission to the default setup is the time and date the command was run.

There is a file in the home directory of all users called the .bashrc file (the period is not a typo – it is a hidden file).  Add the following line to get the ‘history’ output with a time and date stamp:

export HISTTIMEFORMAT="%F %T "

You can enter this at the command line and then run the ‘history’ command to view the new output.

[root@localhost vrussell]# history
531  2009-12-27 21:19:35 cd ..
532  2009-12-27 21:19:36 ls -a
533  2009-12-27 21:20:40 history
534  2009-12-27 21:20:53 export HISTTIMEFORMAT=”%F %T “
537  2009-12-27 21:53:05 grep -lir ‘bashrc’ *
538  2009-12-27 21:53:13 history
539  2009-12-27 21:53:15 clear
540  2009-12-27 21:53:32 vi testScript.sh
541  2009-12-27 21:53:39 history
export HISTSIZE=4000

Another useful history setting is the number of commands that are stored in the .bash_history file.  The default appears to store the last 1000 commands – increase that number to 4000 lines or more.  Grep or [CTRL] + r can filter the results effectively.

. bashrc file entries ( using vi/vim )

# User specific aliases and functions
# display date time in history output
export HISTTIMEFORMAT="%F %T  "
# Increase from default 1000 lines to 4000 lines
export HISTSIZE=4000

# User specific aliases and functions
# display date time in history output
export HISTTIMEFORMAT="%F %T  "
# Increase from default 1000 lines to 4000 lines
export HISTSIZE=4000
alias ll="ls -ltr"

Edit your .bashrc file using pico, nano, vi, vim, emacs. If you can access files via Windows, use Notepad++ (download Notepad++ from SourceForge)  and not Windows default Notepad.  Notepad ++ lets you save files using the Unix line break.