Walker News

How To Display Date And Time In Linux History Command Output?

There is a trick to display timestamp in Linux history command output. With this feature turned on, one can easily understand or review what commands have been executed, on which date at what time, in the Linux system in question.

By default, however, the history command is not enabled to display date and time of each command executed in Red Hat, as with most Linux distributions.

Anyway, it is not complicated to turn on the chronological feature of history command in Linux; even a novice can do it in no time.

The trick to display timestamp in Linux history command output

Configure shell variable HISTTIMEFORMAT to display timestamp of each executed command in the Linux history command output.

To include standard timestamp format in history command output, as shown in above screenshot, all you have to do is login as root to open /etc/profile with vi editor (or any Linux text file editor you’re comfortable with) and add this following line:
export HISTTIMEFORMAT='%F %T >> '

This single magic command line exports the shell environment variable HISTTIMEFORMAT globally, so that every Linux users will able to see their respective command execution history with date and time information.

If you are not system administrator or not a root user in Linux system but would like to see timestamp in history command output, just add that magic line in your own account profile, for example the $HOME/.bash_profile if you’re using Bash Shell. You should able to see the timestamp in history command output after logout and login (or if you execute export HISTTIMEFORMAT='%F %T >> ' in command line).

The value of HISTTIMEFORMAT is a format string used by strftime. If you like to further customize the date/time format in history command output, just run man 3 strftime to get detail of timestamp string format that is valid for HISTTIMEFORMAT.

Custom Search

2018  •  Privacy Policy