Display date & time in Linux history command
One nifty tip to enable timestamps in the history
command in Linux.
When you enter the history
command on a Linux server or macOS terminal, you only see the list of commands but not the date &/or time when those commands were executed. It looks similar to this.
shashank-mbp:~ admin$ history
4 sudo su
5 sudo su
6 sudo su
7 sudo su
8 clear
9 sudo su
10 fish
That is why I use this simple but very useful tip to display the timestamps against all the commands in the output of the history
command. Below is how to enable it.
- Open
.bash_profile
or/etc/profile
file (read the note below).
Edit
/etc/profile
file to enable the timestamps for all the users. If you want to enable it for a particular user, then edit.bash_profile
file of that user.
- Add the below line.
export HISTTIMEFORMAT="%d/%m/%y %T "
%d — date
%m — month
%y — year
%T — time (in hh:mm:ss format)
- Source the file. You can also log out & log back in to reflect the changes.
shashank-mbp:~ admin$ source .bash_profile
If you type history
command now, it will neatly display the timestamps against each command.
507 03/12/20 15:43:08 clear
508 03/12/20 15:43:09 vim .bash_profile
509 03/12/20 15:52:02 source .bash_profile
510 03/12/20 15:52:05 vim .bash_profile
511 03/12/20 15:54:11 uptime
512 03/12/20 15:56:09 history
Even though it looks trivial, it is one of the very first things that I do on a newly created Linux server.
If you like this post, please share it with your friends. I mostly write about DevOps & Linux & am looking forward to posting more.