The Linux command line history is a powerful feature that allows users to track, recall, and reuse previously executed commands in the terminal. Managed primarily by the Bash shell (or other shells like Zsh), the history is stored in a file (typically ~/.bash_history) and can be customized for efficiency. This guide covers setup, formatting, and usage with practical examples.
The command history is stored in ~/.bash_history by default. To verify or change this, check the HISTFILE variable:
echo $HISTFILE
To set a custom history file, add to ~/.bashrc:
export HISTFILE=~/.custom_history
Control the number of commands stored in memory (HISTSIZE) and in the history file (HISTFILESIZE). Add to ~/.bashrc:
export HISTSIZE=1000
export HISTFILESIZE=2000
HISTSIZE limits commands in the current session, while HISTFILESIZE limits the file size.
Customize history behavior using these variables in ~/.bashrc:
HISTCONTROL: Controls how commands are saved.
ignoredups: Ignores duplicate commands.ignorespace: Ignores commands starting with a space.ignoreboth: Combines both.export HISTCONTROL=ignoreboth
HISTTIMEFORMAT: Adds timestamps to history output.export HISTTIMEFORMAT="%F %T "
This formats timestamps as YYYY-MM-DD HH:MM:SS.
By default, history is saved when the session ends. To save after each command, add to ~/.bashrc:
shopt -s histappend
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
histappend appends instead of overwriting, and history -a writes immediately.
The history command displays the command history. With HISTTIMEFORMAT set, it includes timestamps. Example output:
1 2025-10-17 11:00:01 ls -l
2 2025-10-17 11:00:05 cd /tmp
3 2025-10-17 11:00:10 pwd
Customize output further using history | awk or grep for filtering. Example to show only ls commands:
history | grep ls
| Command | Description |
|---|---|
history | Displays the entire command history. |
history n | Shows the last n commands. |
history -c | Clears the current session's history. |
history -w | Writes the current session's history to HISTFILE. |
!n to rerun command number n.
!3
Reruns command number 3 (e.g., pwd).
!!.
!!
!string to rerun the last command starting with string.
!ls
Reruns the most recent ls command.
Ctrl+R, type to search, and press Enter to execute.
Example 1: View recent commands
history 5
Shows the last 5 commands.
Example 2: Search and reuse
!grep
Reruns the most recent grep command.
Example 3: Filter history for analysis
history | grep "cd " | tail -n 5
Shows the last 5 cd commands.
Example 4: Clear history
history -c && history -w
Clears the session history and updates the file.
~/.bash_history to avoid losing it.
cp ~/.bash_history ~/.bash_history_backup
history -a and history -r in PROMPT_COMMAND:
PROMPT_COMMAND="history -a; history -r; $PROMPT_COMMAND"
~/.bashrc:
alias hs='history | grep'
Then use hs ls to search for ls commands.
Linux command line history is a versatile tool for improving productivity. By configuring ~/.bashrc and mastering history commands, users can efficiently manage and reuse commands. Experiment with the examples to tailor history to your workflow.