Linux

The Linux history command: a complete guide to managing command history

This guide thoroughly covers the history command in Linux. You'll learn to search, edit, and clear command history, configure its size and format, significantly speeding up your terminal work.

Updated at February 15, 2026
10-15 min
Easy
FixPedia Team
Применимо к:Ubuntu 22.04+Debian 11+CentOS 8+Fedora 36+Bash 4.0+Zsh 5.0+

Introduction / Why This Matters

The history command is one of the most powerful tools for boosting productivity in the Linux command line. It records every command you've entered, allowing you to:

  • Quickly find and repeat previously used complex commands without retyping.
  • Analyze your actions, for example, to document routine operations.
  • Manage your sessions by clearing unnecessary or confidential entries.

This guide will transform your command history from a simple log into a convenient tool for search and automation.

Prerequisites / Preparation

  1. Terminal access (Ctrl+Alt+T or similar).
  2. Shell: The instructions primarily target bash (standard in most distributions), but most principles apply to zsh as well.
  3. Write permissions in your home directory (files ~/.bash_history or ~/.zsh_history).
  4. A text editor (e.g., nano or vim) for editing configuration files.

Step 1: Viewing and Searching Your History

1.1. Basic Viewing

Simply type the command to see a list of all saved commands with line numbers:

history

By default, it displays the last 500 or 1000 entries (depending on the HISTSIZE setting).

1.2. Searching by Content

To find a command containing specific text (e.g., systemctl), use grep:

history | grep systemctl

This filters only the lines where systemctl appears.

1.3. Searching with Keyboard Shortcuts (Interactive Mode)

  • Ctrl + Rreverse search. Start typing part of a command, and the terminal will show the first match. Press Ctrl + R again to cycle through others. Enter to execute, Ctrl + G to exit search.
  • / — scroll through recent commands.

Step 2: Repeating and Editing Past Commands

2.1. Quick Re-execution

  • !! — execute the last command. Useful after sudo (e.g., sudo !!).
  • !-n — execute the command that was n steps back. !-2 is the second-to-last.
  • !<string> — execute the last command that starts with <string>. For example, !sys runs the most recent command starting with sys.

2.2. Editing Before Execution

  • After finding a command via Ctrl + R, press or to exit search mode and edit the line.
  • Or, find a command with / arrows and edit it like a normal input line.

Step 3: Configuring History Size and Format

By default, your history may be limited. Let's change that.

3.1. Increasing the Number of Saved Commands

Open your shell's configuration file. For bash, this is typically ~/.bashrc:

nano ~/.bashrc

Add or modify the following lines at the end of the file:

# Maximum number of commands in session memory
HISTSIZE=5000
# Maximum number of commands in the history file
HISTFILESIZE=10000

Save (Ctrl+O, Enter) and close (Ctrl+X) the editor. To apply changes in the current session, run:

source ~/.bashrc

3.2. Adding Command Execution Timestamps

For auditing or analysis, it's useful to see when a command was run. Add to ~/.bashrc:

# Format: Year-Month-Day Hour:Minute:Second
export HISTTIMEFORMAT='%F %T '

After source ~/.bashrc, the history command will display the timestamp before each command.

💡 Tip: For zsh, use the HISTTIMEFORMAT variable similarly or configure via setopt EXTENDED_HISTORY.


Step 4: Clearing and Managing the History File

4.1. Clearing the Current Session (RAM)

history -c

This removes all entries from the current session but does not affect the ~/.bash_history file on disk.

4.2. Clearing the History File on Disk

To erase history in the file as well, run two commands:

history -c        # 1. Clear session memory
history -w        # 2. Write (empty) session memory to ~/.bash_history

Warning: This action is irreversible for the current user.

4.3. Deleting a Specific Entry

  1. Find the line number of the command via history.
  2. Delete it:
    history -d <line_number>
    
  3. Immediately sync with the file:
    history -w
    

4.4. Disabling History Recording (Temporarily)

For confidential operations (e.g., typing passwords in plain text, which is highly discouraged), you can temporarily disable recording:

set +o history   # Disable recording for current session
# ... perform confidential commands ...
set -o history   # Re-enable recording

Important: Commands entered while disabled won't appear in history, but they may still be visible in process lists (ps aux) or the terminal's scrollback buffer.


Step 5: Useful Advanced Techniques

5.1. Executing a History Command by Number

!<number>

For example, !123 executes command number 123.

5.2. Substituting Arguments from the Previous Command

  • !$ — the last argument of the previous command. Useful for operations on the same file: cp file.txt backup.txt && mv !$ new_name.txt.
  • !* — all arguments except the first command.

5.3. Exporting History to a File for Analysis

history > my_history.txt

Now you have a text file with the full history for searching or documentation.


Verification

  1. Run several different commands (ls, pwd, echo "test").
  2. Type history — they should appear.
  3. If you configured HISTTIMEFORMAT, verify timestamps are displayed.
  4. Try searching with Ctrl+R — it should find your commands.
  5. Check the history file size: ls -la ~/.bash_history.

If commands appear in history output and persist after closing and reopening the terminal — the configuration is working correctly.


Troubleshooting

ProblemPossible CauseSolution
History is empty after terminal restartHISTSIZE is set to 0 in ~/.bashrc or ~/.profile.Check these files for HISTSIZE=0 and correct it.
Commands run via sudo are not savedHistory is recorded per user by default. Commands executed with sudo may be logged in root's history (/root/.bash_history).To log sudo commands in your history, configure sudo (option timestamp_timeout), but this may be insecure.
History "jumps" or loses entriesThe ~/.bash_history file is corrupted or has incorrect permissions.Ensure you own the file: ls -la ~/.bash_history. If needed, delete the file (it will be recreated) and reconfigure HISTFILESIZE.
history -w does not write changesYou are in a su or sudo su session. The history file is being written as root.Exit the root session (exit) or configure HISTFILE for the root session.

Frequently Asked Questions (FAQ)

Can I synchronize history between multiple terminal windows? Yes, but with caveats. By default, each shell session reads history at startup and writes it on exit. When working in multiple windows simultaneously, recent commands from one window won't appear in another until you exit the first session. To get live updates, you can manually use history -a (append current commands to file) and history -n (read new lines from file), or set up PROMPT_COMMAND in ~/.bashrc:

export PROMPT_COMMAND='history -a; history -n'

How do I find and run a command that contained a typo? Use search based on the part you remember. For example, if you meant to type git commit but typed git comit, search via history | grep comit, get the line number, and run !<number> or simply find it with arrows and correct it.

Is it safe to store all history? No. History is a plain-text file (~/.bash_history), readable by anyone with access to your account. Never type passwords, API keys, or other secrets into the terminal. For confidential operations, disable history (set +o history) or use specialized tools (e.g., ssh-keygen doesn't require password input in history).

Why does history show fewer lines than I expect? Check two variables: echo $HISTSIZE (limit for current session) and echo $HISTFILESIZE (limit for the file). Also ensure you didn't clear history earlier. The ~/.bash_history file might be hidden or in a non-standard location (check echo $HISTFILE).

F.A.Q.

How do I increase the number of commands saved in history?
Why aren't commands being saved to history?
How to safely remove a specific command from history?
Why isn't the command execution time displayed in history?

Hints

Viewing and searching in history
Repeating and editing past commands
Configuring history size and format
Clearing and managing the history file
Disabling history (if required)
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community