Introduction
The history command is a key tool in any Linux environment that allows you to review, reuse and manage the commands you have previously executed in the terminal. Knowing its functioning not only improves productivity, but also helps to avoid errors by repeating complex tasks or to audit what has been done in the system.
What is the history command?
In essence, history is a built-in of most shells (bash, zsh, etc.) that keeps a file where each command line is saved. By default, this history is stored in ~/ .bash _ history for bash and is charged when starting a new session. Each entry receives a sequential number that facilitates its direct reference.
Basic syntax
The simplest way to invoke it is to writehistoryin the prompt. This will show a numbered list of the most recent commands. Some useful options are:
history -c: erases the entire history of the current session.history -d: removes entry number.history -a: annexed the history of the current session to the file ~/ .bash _ history.history -n: load the new lines from the file to the current session history.history -r: Read the history file and upload it to the current session.history -w: writes the current history in the file.
Examples of use
- See the last 20 commands:
history | tail -20 - Find all commands containing the word «git»:
history | grep git - Run command number 134 again:
!134 - Run the last command that started with «ssh»:
!ssh - Run the last command it contained «apt-get update»:
!?apt-get update? - Reuse the last argument from the previous command:
sudo !$
Manage the history size
The environment variablesHISTSIZEandHISTFILESIZEcontrol 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 save up to 10,000 commands in the session and up to 20,000 commands in the file. You can also prevent them from saving duplicate commands or starting with a space using:
export HISTCONTROL=ignoredups:ignorespace
Search in the history
In additiongrep, history itself allows interactive searches withCtrl + r. By pressing that combination, the most recent coincidences appear as you write. You can repeatCtrl + rto step back in the history andEnterto run the found command. This technique is very useful when you don't remember the exact number but it does part of the text.
Delete or modify entries
If you need to remove a specific line because it contains sensitive information, use:
history -d 57
After deletion, it is recommended to rewrite the file with:
history -w
To edit an input, you can delete it and then reenter the corrected command, or use the replacement history expansion:
^foo^bar^
This command replaces "foo" with "bar" in the last command and reruns it.
Conclusion
Dominating the history command transforms the way you interact with the terminal. From rerunning frequent tasks to auditing past actions, their correct configuration and use are essential skills for any system manager, developer or advanced Linux user. Check your specific shell options and adapt your history to your workflows to gain efficiency and security.


