To display your Linux command history with date and time, use the built-in history command with the -f flag, or set the HISTTIMEFORMAT environment variable. The simplest one-time command is history -f, which prints each command with a timestamp showing when it was executed.
Why Timestamps Are Missing by Default
When you type history in a terminal, you normally see only a numbered list of commands like 123 ls -la. The shell stores the command text and a sequence number, but it does not record the execution time unless you explicitly ask it to. This is a deliberate design choice: storing timestamps adds overhead to every command, and early Unix shells did not have this feature at all. The Bash shell, which is the default on most Linux distributions, has supported timestamps since version 3.0 (released in 2004), but the feature is off by default to keep the history file simple and compatible with older tools.
How HISTTIMEFORMAT Works
The key to persistent timestamps is the HISTTIMEFORMAT variable. When you set this variable to a format string, Bash automatically prepends a timestamp to every new command it saves. The format follows the same rules as the date command's + format specifiers. For example:
%F– full date in YYYY-MM-DD format%T– time in HH:MM:SS 24-hour format%c– complete date and time in locale-specific format%d/%m/%Y %H:%M:%S– custom European-style date and time
To make this permanent, add the following line to your ~/.bashrc file:
export HISTTIMEFORMAT="%F %T "
After saving the file, run source ~/.bashrc or open a new terminal. From this point forward, every new command you type will be saved with its timestamp. Note that this only affects commands executed after the change; commands already in your history file will not gain timestamps retroactively.
One-Time Timestamps with history -f
If you do not want to permanently change your shell configuration, you can view timestamps for the current session or for an existing history file using the -f flag. The command history -f displays the history with the timestamp format defined by HISTTIMEFORMAT. If HISTTIMEFORMAT is not set, history -f will show the raw Unix epoch timestamps (seconds since January 1, 1970), which are not human-readable. To get readable output, set the variable just for that command:
HISTTIMEFORMAT="%F %T " history -f
This prints the entire history with dates and times without modifying your configuration. It works on the history file that Bash loaded at startup, so it includes commands from previous sessions if your shell is configured to save them.
Setting the Format for the Current Session
You can also set HISTTIMEFORMAT temporarily for the current terminal session without editing any files. Simply type:
export HISTTIMEFORMAT="%d/%m/%y %H:%M:%S"
Then run history normally. The output will include timestamps for all new commands you type after setting the variable. Commands typed before the export will not have timestamps because they were already saved without them. This is useful when you want timestamps only for a short debugging session or for a specific task, and you do not want to permanently alter your shell startup files.
Viewing Timestamps in .bash_history
The history file itself, usually ~/.bash_history, stores timestamps as comment lines beginning with # followed by the Unix epoch time. For example:
#1712345678
ls -la
#1712345700
cd /var/log
You can view these raw values with cat ~/.bash_history or tail -50 ~/.bash_history. To convert them to readable dates, you can use the date command manually, but this is tedious for many entries. A simpler approach is to use history -f with HISTTIMEFORMAT set, which reads these comment lines and formats them automatically. Note that if you edit the history file manually, you must keep the comment lines directly above the command lines for Bash to associate the timestamp with the correct command.
Common Problems and Misconceptions
One frequent issue is that timestamps do not appear for commands from before you set HISTTIMEFORMAT. This is expected behavior, not a bug. Another common problem is that history -f shows epoch numbers instead of readable dates. This happens when HISTTIMEFORMAT is empty or unset. A third issue is that some terminal multiplexers like screen or tmux may not save history correctly if they are configured to use their own history mechanisms. If you use Zsh instead of Bash, the syntax is different: you set HISTFILE and use fc -l with the -d flag, or set EXTENDED_HISTORY option. A common misconception is that history itself stores timestamps by default; it does not, and you must explicitly enable them.
Comparing Bash, Zsh, and Fish
Bash uses HISTTIMEFORMAT as described above. Zsh, the default shell on macOS and popular among Linux power users, stores timestamps when the EXTENDED_HISTORY option is enabled. You can turn it on by adding setopt EXTENDED_HISTORY to your ~/.zshrc. Zsh's history format includes the elapsed time in seconds in addition to the start time. Fish shell, which is designed to be user-friendly, includes timestamps by default in its history file, stored in a binary format under ~/.local/share/fish/fish_history. The history command in Fish shows timestamps automatically. If you use multiple shells, be aware that each has its own history file and its own timestamp mechanism.
Alternatives for Detailed Auditing
If you need more detailed logging than what HISTTIMEFORMAT provides, consider using script to record entire terminal sessions, or auditd for system-wide command auditing. The script command saves a typescript of everything printed to the terminal, including command prompts and output. For security auditing, auditd can log every execve system call with full timestamps, but it requires root privileges and careful configuration. Another lightweight option is to add a trap to your PROMPT_COMMAND in Bash that logs each command with a timestamp to a separate file, giving you a custom audit trail without modifying the standard history. For most users, however, HISTTIMEFORMAT is sufficient and is the recommended approach because it is built-in, lightweight, and requires no extra tools.
Frequently Asked Questions
Why does my history show the same timestamp for many commands?
This usually happens when you set HISTTIMEFORMAT after commands were already saved. Commands saved before the variable was set do not have timestamps, so Bash might display them with the timestamp of the first command that does have one, or with the current time depending on your Bash version. The fix is to start a new shell or clear your history after setting the variable.
Can I add timestamps to my existing history file?
No, not automatically. The timestamps are stored as comment lines in the history file, and there is no built-in way to reconstruct the execution time for commands that were saved without them. You can manually edit the file to add epoch timestamps, but this is error-prone and not recommended.
Does HISTTIMEFORMAT affect the history command output only, or also the file?
It affects both. When set, new commands are saved to the history file with the timestamp comment lines, and the history command displays them using the format. If you unset the variable, the timestamps remain in the file but are not displayed.
What is the difference between history -f and history without flags?
The -f flag forces Bash to read the history file and display timestamps according to HISTTIMEFORMAT. Without it, history shows the in-memory list for the current session, which may not include timestamps even if the file has them, depending on how the session was started.
Can I use colors or bold text in the timestamp format?
Yes, you can include ANSI escape codes in HISTTIMEFORMAT. For example, export HISTTIMEFORMAT="\e[1;32m%F %T\e[0m " will display the timestamp in bold green. However, this makes the history file harder to read with plain text tools, and the codes may not display correctly in all terminals.
Does this work in a Docker container or over SSH?
Yes, it works in any environment where Bash is running, including Docker containers and SSH sessions. However, if the container is ephemeral, changes to ~/.bashrc will be lost when the container stops. For SSH sessions, the timestamp format follows the user's shell configuration on the remote server.