Managing your command history in Linux is essential for improving productivity, automating tasks, and troubleshooting issues. Regardless of your Linux distribution or shell (such as Bash or Zsh), Linux provides powerful tools to view, search, and manage the history of your terminal commands. This guide walks you through everything you need to know.
Linux automatically records the commands you type into the terminal in a chronological list called the command history. This makes it easy to repeat previous commands, fix typos, or locate a command you ran previously.
By default, command history is stored in:
This is ideal for quickly accessing recent actions.
Reverse search allows you to dynamically search through past commands as you type:
Use the history command to list past commands:
bash
CopyEdit
history
This displays a numbered list of your recent commands (typically the last 1,000), with the oldest at the top and the most recent at the bottom.
To view a specific number of recent commands:
bash
CopyEdit
history 20
This shows only the last 20 commands.
If you’re looking for a specific command or string, pipe the output of history into grep:
bash
CopyEdit
history | grep "apt"
This filters and displays only the commands containing the word "apt".
Each history entry has a reference number. You can rerun any past command using this number:
bash
CopyEdit
!1045
This re-executes command number 1045 from the history list.
These shortcuts save time and typing:
To remove a specific command from the history:
bash
CopyEdit
history -d [number]
Replace [number] with the ID of the command you want to remove.
To clear all commands from your current terminal session:
bash
CopyEdit
history -c
To also clear your saved history file:
bash
CopyEdit
> ~/.bash_history
You can open the full history file in a text editor:
bash
CopyEdit
nano ~/.bash_history
Or use cat, less, or tail to inspect the contents:
bash
CopyEdit
cat ~/.bash_history
Sometimes the history file isn’t updated until the session ends. To manually save the session history:
bash
CopyEdit
history -a
You can configure your shell to ignore commands starting with a space:
bash
CopyEdit
export HISTCONTROL=ignorespace
Then run a command like this (with a space before it):
bash
CopyEdit
ls -la
This command won't be saved to history.
To avoid logging commands during a session:
bash
CopyEdit
unset HISTFILE
This prevents commands from being written to the history file until the terminal is closed or the variable is reset.
Task
Command / Shortcut:
View history
history
View last N commands
history N
Search history
`history
Reverse search
Ctrl + R
Rerun last command
!!
Rerun command by number
!123
Delete a specific entry
history -d 123
Clear all session history
history -c
Save current session to file
history -a
View history file
nano ~/.bash_history
By mastering these command history techniques, you can significantly improve your workflow in any Linux terminal. These tools and shortcuts are essential for efficient command-line usage, script creation, and day-to-day system management.
If you’re still having trouble, consider reaching out to Support.Com for a personalized solution to all technical support issues.