How to Use the History Command in Linux
The history command in a Linux terminal is a built-in
shell utility that tracks, records, and displays a list of previously
executed commands. This article explains how the history
command works, how to navigate and search your terminal records, methods
for re-executing past commands without retyping them, and how to manage
or clear your command log to improve productivity and security.
What the History Command Does
Whenever you run a command in a Linux shell like Bash or Zsh, the
shell appends that entry to an internal log. When you enter
history into the terminal, it outputs a chronological,
numbered list of these past commands. This allows users to review
previous system changes, audit their workflows, or retrieve complex
commands used earlier.
Viewing Command History
Running the command by itself displays the entire stored history:
historyBecause this list can contain hundreds or thousands of lines, you can limit the output to the most recent entries by appending a number. For example, to view only the last 10 commands:
history 10You can also pipe the output to a pager like less to
scroll through the entries one screen at a time:
history | lessRe-executing Previous Commands
The history command integrates with event designators
(the exclamation mark !) to quickly rerun commands without
retyping them:
- Rerun the last command: Enter
!!to repeat the immediately preceding command. This is often used withsudoif you forgot root privileges (e.g.,sudo !!). - Run by line number: Enter
!followed by the specific number from the history list (e.g.,!42to run line 42). - Run the most recent command starting with a string:
Enter
!followed by the command name (e.g.,!gitto rerun the most recent command that began withgit). - Run a relative command: Enter
!-nto execute the command run n steps ago (e.g.,!-2to rerun the command from two steps back).
Searching Through History
Instead of scrolling through extensive lists, you can locate specific commands using two primary methods:
- Piping to Grep: Filter historical commands by
keyword using standard text utilities:
history | grep "ssh" - Reverse Interactive Search: Press
Ctrl + Rin the terminal and begin typing any part of the command you want to locate. The shell displays matching commands interactively; pressEnterto run the found command, or pressCtrl + Ragain to cycle backward through older matches.
Managing and Clearing History
By default, commands in the current session are stored in memory and
written to a file in your home directory (such as
~/.bash_history) upon exiting the terminal. You can manage
this file and your active session memory with specific flags:
- Delete a specific entry: Remove an individual
command by specifying its line number with the
-dflag:history -d 150 - Clear session history: Empty the current session's
memory buffer with the
-cflag:history -c - Write changes to disk: Force the current session's
memory buffer to overwrite the history file on disk:
history -w
Configuring History Behavior
You can customize how history operates by editing shell variables in
your ~/.bashrc file. Key variables include:
HISTSIZE: The maximum number of commands stored in active memory during a session.HISTFILESIZE: The maximum number of lines retained in the history file on disk.HISTCONTROL: Controls whether duplicate lines or commands preceded by a space are ignored (e.g.,ignorebothprevents commands starting with a space from being recorded, useful for sensitive credentials).