Viewing Command History with Timestamps Across Different Platforms

December 22, 2025

Overview

By default, most shells don't display timestamps when you run the history command. However, timestamp information can be crucial for debugging, auditing, or understanding when commands were executed. This guide covers how to enable and view command history with full date/time stamps across different platforms and shells.

Unix/Linux and macOS

Zsh (Default on Modern macOS)

Quick View (Current Session)

If EXTENDED_HISTORY is already enabled, view history with timestamps:

# Display history with ISO8601 timestamp format (yyyy-mm-dd hh:mm)
history -i 1

# Or use European format (dd.mm.yyyy hh:mm)
history -E 1

Example output:

2025-12-22 10:23  ls -la
2025-12-22 10:24  cd ~/Documents
2025-12-22 10:25  git status

Permanent Configuration

To enable timestamps for all future sessions:

Step 1: Edit the Zsh configuration file

nano ~/.zshrc

Step 2: Add the following line

setopt EXTENDED_HISTORY

Optional: Additional history settings

# Enable extended history with timestamps
setopt EXTENDED_HISTORY

# Share history across all terminal sessions
setopt SHARE_HISTORY

# Append to history file immediately, not on shell exit
setopt INC_APPEND_HISTORY

# Don't record duplicate commands
setopt HIST_IGNORE_DUPS

# Remove older duplicate entries from history
setopt HIST_IGNORE_ALL_DUPS

# Increase history size
HISTSIZE=10000
SAVEHIST=10000
HISTFILE=~/.zsh_history

Step 3: Apply the changes

source ~/.zshrc

Step 4: View history with timestamps

# View all history with timestamps
history -i 1

# View last 20 commands with timestamps
history -i -20

# Search history with timestamps
history -i 1 | grep "git"

Understanding Zsh History File Format

With EXTENDED_HISTORY enabled, the ~/.zsh_history file stores timestamps in epoch format:

cat ~/.zsh_history

Example content:

: 1703251415:0;ls -la
: 1703251430:0;cd ~/Documents
: 1703251445:0;git status

Format: : <epoch_timestamp>:<duration>;<command>

Bash (Linux and Older macOS)

Quick View (Current Session)

# Set the timestamp format temporarily
export HISTTIMEFORMAT="%F %T "

# View history with timestamps
history

Example output:

1  2025-12-22 10:23:15 ls -la
2  2025-12-22 10:24:30 cd ~/Documents
3  2025-12-22 10:25:45 git status

Permanent Configuration

Step 1: Edit the Bash configuration file

# For interactive non-login shells
nano ~/.bashrc

# For login shells (macOS typically uses this)
nano ~/.bash_profile

Step 2: Add the following lines

# Set timestamp format: YYYY-MM-DD HH:MM:SS
export HISTTIMEFORMAT="%F %T "

# Increase history size
export HISTSIZE=10000
export HISTFILESIZE=20000

# Avoid duplicate entries
export HISTCONTROL=ignoredups:erasedups

# Append to history file, don't overwrite
shopt -s histappend

# Save multi-line commands as one entry
shopt -s cmdhist

Step 3: Apply the changes

source ~/.bashrc
# Or: source ~/.bash_profile

Step 4: View history

# View all history
history

# View last 20 commands
history 20

# Search history
history | grep "git"

Understanding Bash History File Format

With HISTTIMEFORMAT set, the ~/.bash_history file stores timestamps as separate lines:

cat ~/.bash_history

Example content:

#1703251415
ls -la
#1703251430
cd ~/Documents
#1703251445
git status

Format: Lines starting with # followed by epoch timestamp, then the command on the next line.

HISTTIMEFORMAT Format Codes

You can customize the timestamp format using these codes:

CodeDescriptionExample
%FFull date (YYYY-MM-DD)2025-12-22
%TFull time (HH:MM:SS)14:30:15
%DShort date (MM/DD/YY)12/22/25
%YYear (4 digits)2025
%mMonth (01-12)12
%dDay of month (01-31)22
%HHour (00-23)14
%MMinute (00-59)30
%SSecond (00-59)15
%sEpoch timestamp1703251415

Examples:

# US format: MM/DD/YY HH:MM:SS AM/PM
export HISTTIMEFORMAT="%m/%d/%y %I:%M:%S %p "

# European format: DD-MM-YYYY HH:MM
export HISTTIMEFORMAT="%d-%m-%Y %H:%M "

# ISO 8601 format: YYYY-MM-DDTHH:MM:SS
export HISTTIMEFORMAT="%FT%T "

# Simple: YYYY-MM-DD HH:MM
export HISTTIMEFORMAT="%F %R "

Windows

PowerShell

PowerShell has built-in support for command history with timestamps, but it's not displayed by default.

View History with Timestamps (Session Only)

PowerShell's Get-History cmdlet includes timing information:

# View history with execution time
Get-History

# View with custom format including timestamp
Get-History | Format-Table -Property Id,
  @{Label="StartTime";Expression={$_.StartExecutionTime}},
  CommandLine -AutoSize

# View last 10 commands with timestamps
Get-History -Count 10 | Format-Table Id, StartExecutionTime, CommandLine -AutoSize

Example output:

Id StartExecutionTime    CommandLine
-- ------------------    -----------
 1 12/22/2025 2:30:15 PM ls
 2 12/22/2025 2:31:42 PM cd Documents
 3 12/22/2025 2:32:10 PM git status

Enhanced History Function

Add this function to your PowerShell profile for easier access:

Step 1: Open your PowerShell profile

# Check if profile exists
Test-Path $PROFILE

# Create if it doesn't exist
if (!(Test-Path $PROFILE)) {
    New-Item -Path $PROFILE -ItemType File -Force
}

# Edit the profile
notepad $PROFILE

Step 2: Add this function

function Get-HistoryWithTime {
    param(
        [int]$Count = 50
    )
    Get-History -Count $Count | Format-Table -Property Id,
        @{Label="Date";Expression={$_.StartExecutionTime.ToString("yyyy-MM-dd")}},
        @{Label="Time";Expression={$_.StartExecutionTime.ToString("HH:mm:ss")}},
        @{Label="Duration";Expression={$_.EndExecutionTime - $_.StartExecutionTime}},
        CommandLine -AutoSize
}

# Create an alias
Set-Alias -Name hh -Value Get-HistoryWithTime

Step 3: Reload your profile

. $PROFILE

Step 4: Use the function

# View last 50 commands with timestamps
hh

# View last 20 commands
hh -Count 20

Example output:

Id Date       Time     Duration         CommandLine
-- ----       ----     --------         -----------
 1 2025-12-22 14:30:15 00:00:00.1234567 ls
 2 2025-12-22 14:31:42 00:00:00.0987654 cd Documents
 3 2025-12-22 14:32:10 00:00:01.5432109 git status

Persistent History with PSReadLine

PowerShell 5.0+ includes PSReadLine module which saves history across sessions:

# View persistent history file location
(Get-PSReadLineOption).HistorySavePath

# Typical location:
# C:\Users\<username>\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt

# View the content
Get-Content (Get-PSReadLineOption).HistorySavePath

# Search persistent history
Get-Content (Get-PSReadLineOption).HistorySavePath | Select-String "git"

Note: PSReadLine history file doesn't include timestamps by default. Only Get-History shows timestamps for the current session.

Export History with Timestamps

# Export to CSV
Get-History | Select-Object Id,
    @{Name="DateTime";Expression={$_.StartExecutionTime}},
    CommandLine | Export-Csv -Path "~/history.csv" -NoTypeInformation

# Export to JSON
Get-History | Select-Object Id, StartExecutionTime, EndExecutionTime, CommandLine |
    ConvertTo-Json | Out-File "~/history.json"

CMD (Command Prompt)

Traditional Windows Command Prompt (cmd.exe) does not support command history timestamps natively. However, you can use the doskey command to view history for the current session:

doskey /history

Limitations:

  • No timestamps available
  • History is not persistent across sessions
  • Limited to current cmd.exe window

Alternative: Use PowerShell instead for better history management with timestamps.

Comparison Table

ShellPlatformTimestamp SupportConfiguration FileView Command
ZshmacOS, Linux✅ Yes (with EXTENDED_HISTORY)~/.zshrchistory -i 1
BashLinux, macOS, WSL✅ Yes (with HISTTIMEFORMAT)~/.bashrc or ~/.bash_profilehistory
PowerShellWindows, macOS, Linux✅ Yes (built-in)$PROFILEGet-History
CMDWindows❌ NoN/Adoskey /history

Important Limitations

⚠️ Commands executed BEFORE enabling timestamps will NOT have accurate timestamps.

  • Zsh: Old commands may show epoch date (1970-01-01)
  • Bash: Old commands may show the time you enabled HISTTIMEFORMAT
  • PowerShell: Only tracks current session unless using PSReadLine

Only new commands entered after the configuration change will have correct timestamps.

Advanced Tips

Search History with Date Range (Bash)

# View history from a specific time range
history | awk '$2 >= "2025-12-22" && $2 <= "2025-12-23"'

Export Bash History with Timestamps

# Export to file
HISTTIMEFORMAT="%F %T " history > ~/command_history.txt

# Export as CSV
echo "Timestamp,Command" > ~/history.csv
HISTTIMEFORMAT='"%F %T",' history | sed 's/^ *[0-9]* *//' >> ~/history.csv

Cross-Platform History Script

For teams working across different platforms, consider standardizing on this approach:

# Add to ~/.bashrc, ~/.zshrc, or $PROFILE
alias h='history_with_time'

# Bash/Zsh version
history_with_time() {
    if [ -n "$ZSH_VERSION" ]; then
        history -i 1 | tail -${1:-50}
    else
        HISTTIMEFORMAT="%F %T " history ${1:-50}
    fi
}

Security Considerations

  • History files may contain sensitive information (passwords, API keys, etc.)
  • Never commit history files to version control
  • Use HISTCONTROL=ignorespace (Bash) to exclude commands starting with space
  • Clear sensitive commands from history:
# Bash: Remove specific line
history -d <line_number>

# Bash: Clear all history
history -c

# Zsh: Edit history file directly
nano ~/.zsh_history

# PowerShell: Clear current session
Clear-History

Troubleshooting

Timestamps Not Showing

Problem: History command doesn't show timestamps after configuration.

Solutions:

  1. Reload configuration:

    # Bash
    source ~/.bashrc
    
    # Zsh
    source ~/.zshrc
    
    # PowerShell
    . $PROFILE
  2. Open new terminal window/tab

  3. Verify configuration was saved:

    # Bash
    grep HISTTIMEFORMAT ~/.bashrc
    
    # Zsh
    grep EXTENDED_HISTORY ~/.zshrc
  4. Check if variable is set:

    # Bash
    echo $HISTTIMEFORMAT
    
    # Zsh
    setopt | grep EXTENDED_HISTORY

History Not Persisting Across Sessions

Problem: History is lost when closing the terminal.

Solutions:

Bash:

# Ensure these settings are in ~/.bashrc
shopt -s histappend
export HISTSIZE=10000
export HISTFILESIZE=20000

Zsh:

# Ensure these settings are in ~/.zshrc
setopt APPEND_HISTORY
setopt INC_APPEND_HISTORY
SAVEHIST=10000

PowerShell:

# Install/update PSReadLine module
Install-Module PSReadLine -Force

References

  1. Zsh Documentation. (2025). Options - 16.2.4 History. Retrieved December 22, 2025, from https://zsh.sourceforge.io/Doc/Release/Options.html
  2. GNU Bash Manual. (2025). Bash History Facilities. Retrieved December 22, 2025, from https://www.gnu.org/software/bash/manual/html_node/Bash-History-Facilities.html
  3. Microsoft PowerShell Documentation. (2025). about_History. Retrieved December 22, 2025, from https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_history
  4. PowerShell Gallery. (2025). PSReadLine Module. Retrieved December 22, 2025, from https://www.powershellgallery.com/packages/PSReadLine

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-12-22). Viewing Command History with Timestamps Across Different Platforms. https://caiborui.com/blog/2025-12-22-en-command-history-timestamps-cross-platform

@misc{borui2025-ab657f79,
  author = {borui},
  title = {{Viewing Command History with Timestamps Across Different Platforms}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-12-22-en-command-history-timestamps-cross-platform}
}