Skip to content
Home ยป How to Display the Timestamp in the History Command?

How to Display the Timestamp in the History Command?

  • by

Displaying the timestamp in the history command is very useful for various purposes. However, in general, Linux systems do not display a timestamp when you run the history command

 

Problem

How to display the timestamp in the history command?

 

Solution

If you type the history command on your Linux server, by default, you will find that there is no timestamp, as in the image below:

display the timestamp in the history command
The history command

 

So that your Linux server can display timestamps in the history command, type the command below:

echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bashrc
source ~/.bashrc
display the timestamp in the history command
The history command with a timestamp

 

The image above shows that the timestamp is already visible when you type the history command. Linux commands executed for a long time will display the same timestamp (look at the image above in the red box). However, if you run another Linux command, the timestamp displayed will be the same as when you executed the Linux command (look at the image above in the green box).

 

Note

By default, you have to run the commands above on each user to display the timestamps in the history command. But I think it’s very tiring to do that. So, if you want to display a timestamp in the history command for each Linux user, copy the command below:

sudo vi /etc/profile.d/history-timestamp.sh

 

After that, copy the script below into the file:

export HISTTIMEFORMAT="%F %T "

 

and then run the below script:

sudo chmod 644 /etc/profile.d/history-timestamp.sh

 

The history command in the new user’s shell will display timestamps automatically if there is a new user on your Linux server.

 

References

cyberciti.biz
stackoverflow.com
tecmint.com
linuxhandbook.com

image_pdfimage_print
Visited 45 times, 1 visit(s) today
Tags:

Leave a Reply

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