The history command in Linux: how to use and manage your command history

Introduction

The history command is a fundamental tool in any Linux environment that allows you to review, reuse, and manage the commands you have previously executed in the terminal. Knowing how it works not only improves productivity, but also helps avoid errors when repeating complex tasks or auditing what has been done on the system.

What is the history command?

In essence, history is a built‑in of most shells (bash, zsh, etc.) that maintains a file where each command line entered is saved. By default, this history is stored in ~/.bash_history for bash and is loaded when starting a new session. Each entry receives a sequential number that makes it easy to reference directly.

Basic syntax

The simplest way to invoke it is to type history at the prompt. This will display a numbered list of the most recent commands. Some useful options are:

  • history -c : clears the entire history of the current session.
  • history -d : deletes entry number .
  • history -a : appends the history of the current session to the ~/.bash_history file.
  • history -n : loads new lines from the file into the current session’s history.
  • history -r : reads the history file and loads it into the current session.
  • history -w : writes the current history to the file.

Usage examples

  1. View the last 20 commands: history | tail -20
  2. Search for all commands containing the word “git”: history | grep git
  3. Re‑execute command number 134: !134
  4. Execute the last command that started with “ssh”: !ssh
  5. Execute the last command that contained “apt-get update”: !?apt-get update?
  6. Reuse the last argument of the previous command: sudo !$

Managing history size

The environment variables HISTSIZE and HISTFILESIZE control how many lines are kept in memory and in the file, respectively. For example, add to ~/.bashrc:

export HISTSIZE=10000
export HISTFILESIZE=20000

This will store up to 10 000 commands in the session and up to 20 000 in the file. You can also avoid saving duplicate commands or those that start with a space using:

export HISTCONTROL=ignoredups:ignorespace

Searching the history

In addition to grep, history itself allows interactive searches with Ctrl+r. Pressing that combination shows the most recent matches as you type. You can repeat Ctrl+r to go further back in the history and Enter to execute the found command. This technique is very useful when you don’t remember the exact number but do remember part of the text.

Deleting or modifying entries

If you need to delete a specific line because it contains sensitive information, use:

history -d 57

After deleting, it is advisable to rewrite the file with:

history -w

To edit an entry, you can delete it and then re‑enter the corrected command, or you can use history expansion with substitution:

^foo^bar^

This command replaces “foo” with “bar” in the last command and re‑executes it.

Conclusion

Mastering the history command transforms the way you interact with the terminal. From re‑executing frequent tasks to auditing past actions, its proper configuration and use are essential skills for any system administrator, developer, or advanced Linux user. Explore the options of your specific shell and adapt the history to your workflows to gain efficiency and security.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .