You can check Linux command history with timestamp. Let us learn how to check command history in Linux with date for all users.
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.