By default, the Bash history command shows:
-
The command number
-
The command itself
β Missing: Timestamp of when the command was executed
β
Solution: Use the HISTTIMEFORMAT environment variable
β Enable Timestamps in History
Set the following variable in your shell:
HISTTIMEFORMAT="%d/%m/%y %T "
π Breakdown of the format:
Token | Meaning |
|---|---|
| Day (e.g., 14) |
| Month (e.g., 04) |
| Year (e.g., 25) |
| Time (HH:MM:SS) |
π§ͺ Test It Out
HISTTIMEFORMAT="%d/%m/%y %T "
history
βοΈ You'll now see the date and time for each command listed.
β Note:
-
Commands before setting
HISTTIMEFORMATwill show the same timestamp (the moment you set the variable). -
Bash didnβt record execution times earlier, so it canβt retroactively apply them.
π οΈ Make It Persistent
To ensure the setting survives logout or reboot, add it to your ~/.bashrc file:
echo 'HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bashrc
Then reload your shell:
source ~/.bashrc
π Verify:
cat ~/.bashrc
β The last line should contain your HISTTIMEFORMAT setting.