How to see the history command with timestamp in Linux. Users can see history command in Linux with date; Linux history command with timestamp and user.
Linux History Command With Timestamp And User
To enable timestamps in the history command, you have to export the HISTTIMEFORMAT variable using the following command:
export HISTTIMEFORMAT="%F %T "
In the above command, %F displays the date in YYYY-MM-DD format and %T displays the time in HH:MM:SS format.
If you want to also view username in command history, run the pstree command in the following manner:
$ original_user=${SUDO_USER:-$(pstree -Alsu "$$" | sed -n "s/.*(\([^)]*\)).*($USER)[^(]*$/\1/p")} $ export HISTTIMEFORMAT="<%F %T> (${original_user:-$USER}) [$$] "
NOTE: The above command works for the current session. Once you logout it will reset to the default format of your system.
To permanently change the history format, you will have to edit ~/.bashrc file.
Open the file using the following command:
$ vi ~/.bashrc
Now add the following line to set the history entry’s format:
export HISTTIMEFORMAT=”%F %T “
Now save and close the file.