If you’re a command-line enthusiast, you probably use the history command regularly to recall past commands. It’s a handy feature that allows you to review your actions, streamline your workflow, and even troubleshoot if needed. However, there’s a way to supercharge your history logs by enabling timestamps. By default, history command stores last one thousand commands in their output.
In this blog post, we’ll walk you through the process of enabling timestamps in the history command, giving you a more comprehensive and organized view of your command-line journey.
Why Timestamps Matter:
By default, the history command in Linux & Unix only displays the list of commands with line numbers, making it challenging to determine when you executed each command. Enabling timestamps in your history logs can significantly enhance your productivity and help you gain insights into how you spend your time on the terminal. Timestamps offer a clearer perspective on your workflow, assisting you in tracking when specific tasks were performed, and identifying potential bottlenecks.
Enabling Timestamp in History Command
Sample Output of History Command,
As we can see in above history command output what commands were executed but issue is here that we do not have timestamp in output. So, we can’t say anything about the timings of command execution.
So, to enable timestamp in history command output, we must configure “HISTTIMEFORMAT” shell variable.
Execute following command to configure this variable
# export HISTTIMEFORMAT="%F %T "
- %F –> shows Date in the format ‘YYYY-M-D’ (Year-Month-Day)
- %T –> shows Time in the format ‘HH:MM:S’ (Hour:Minute:Seconds)
Now execute the history command again and verify whether we can see date and time in front of each command,
That’s it we have successfully enable timestamp in history command. To make “HISTTIMEFORMAT” variable persistent across the reboot, configure it permanently in ~.bashrc file , append the following code at end of file
# vi ~/.bashrc ………… export HISTTIMEFORMAT="%F %T " …………
Save and exit the file.
To make changes of bashrc file into the effect immediately, execute beneath command,
# source ~/.bashrc
In case if you wish to remove timestamp from history command then remove the line which contains “export HISTTIMEFORMAT=”%F %T ” from ~/.bashrc file
If you want to enable timestamp in history command for all local users too, then define the variable HISTTIMEFORMAT in /etc/profile file instead of root user’s ~/.bashrc file.
# vi /etc/profile …………… export HISTTIMEFORMAT="%F %T " …………
Save and exit the file. To make above changes into the effect , source it.
# source /etc/profile
Now run history command via local user and see whether date and time is visible in front of each command.
[root@mail ~]# su - linuxtechi [linuxtechi@mail ~]$ history 1 2019-10-28 01:07:46 exit 2 2019-10-28 01:07:46 exit 3 2019-10-28 01:07:46 sudo vi /etc/fstab 4 2019-10-28 01:07:46 vi /etc/ntp.conf 5 2019-10-28 01:07:46 exit 6 2019-10-28 01:07:46 echo $JAVA_HOME 7 2019-10-28 01:07:46 exit ………
That’s all from this article. Please do share it among your technical friends and share your queries and feedback in below comments section.