Bash History: Display Date Time For Linux Commands
In Linux, we need to know history with its exact time. But in Linux like RHEL or CentOS history doesn’t contain time. So by default it is not possible to know history time in Linux. But we can make it possible with a small change.
This is default view of history command.
[user@local-srv11 ~]$ history 1 ls 2 ll 3 history 4 ping gmail.com 5 stat /etc/hosts 6 cd /var/lib 7 ls 8 cd libvirt/ 9 ls 10 df -hl 11 ip a 12 cd 13 history
Here we can see there is no information for any of commands time, so in case you like to track time with events, it would bit complex and difficult to find out the exact time of process and command run by user.
What we can do, edit ~/.bash_profile of user, this will provide you time infomration for that user only.
[user@local-srv11 ~]$ cat .bash_profile # .bash_profile# Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export HISTTIMEFORMAT="%T %d-%b-%Y " export PATH
You need to logout user and login again or use command source ~/.bash_profile
In case you want to make it for complete system, then edit /etc/bashrc
#echo "export HISTTIMEFORMAT="%T %d-%b-%Y "" >> /etc/bashrc
We can use various formats mentioned in man page in date comand. Some examples mentioned below.
%b locale’s abbreviated month name (e.g., Jan)
%B locale’s full month name (e.g., January)
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour ( 0..23)
%l hour ( 1..12)
%m month (01..12)
%M minute (00..59)