Add a Timestamp to Bash Command History on Linux

PowerADM.com / Linux / CentOS / Add a Timestamp to Bash Command History on Linux

On Linux, a history of recent console commands is stored in the .bash_history file. You can view the contents of this file with any text editor or with the command:

$ history

Bash history on LInux

The command will output a numbered list of all the commands you have previously run. For convenience, you can add the timestamp (date and time) of the command execution to the bash history.

To do this, you need to add the following parameters to the ~/.bashrc file.

$ mcedit $HOME/.bashrc

export HISTTIMEFORMAT='%Y-%m-%d %H:%M:%S '

Add HISTTIMEFORMAT to bash_profile

Or use:

$ echo 'export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "' >> ~/.bash_profile

Additionally, you can increase the size of the .bash_history file to 2000 lines:

export HISTSIZE=2000

A list of commands that do not need to be saved to the bash history can be added to the histignore list:

export HISTIGNORE="ls:ll:history:w:htop"

To apply the changes, you need to re-login or run the command:

$ source ~/.bashrc

The date and time will now be displayed opposite the command in the bash history.

 Show A timestamp (Date / Time) in Bash history .

If bash-completion is not working in Linux, you can fix it.
Leave a Reply

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