πŸ•’ Recording Date and Time for Each Command in Bash History

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

%d

Day (e.g., 14)

%m

Month (e.g., 04)

%y

Year (e.g., 25)

%T

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 HISTTIMEFORMAT will 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.

Updated on