📜 Command: history (View Command History)
The history command displays a numbered list of all the commands you have typed in your current and previous terminal sessions.
1. The "Why"
As you work on complex tasks—like configuring your Linux bootloader, debugging code for Preader, or running intricate find commands—you will often forget the exact syntax of a command that worked perfectly 20 minutes ago.
history saves you from having to re-memorize or re-type long strings. It is the "memory" of your terminal.
2. Basic Syntax
history
Running this will output a long list of commands, each with a number next to it.
3. How to Use the Results (The "Bang" Commands)
The numbers next to the history entries aren't just for show; you can use them to re-run commands instantly.
| Shortcut | Action |
|---|---|
!! |
Repeat the very last command (often used as sudo !!). |
!n |
Repeat command number n from the list. |
!-n |
Repeat the command that was n steps ago. |
!grep |
Repeat the most recent command that started with "grep". |
4. Practical Examples
A. Finding a Specific Past Command
If you know you ran a complicated pacman command earlier but can't find it, pipe history into grep:
history | grep "pacman"
B. Limiting the Output
If you only want to see the last 15 commands you ran:
history 15
C. Clearing Your History
If you typed something sensitive (like a password in a command argument) and want to wipe the history for the current session:
history -c
D. Searching History Interactively
This is a pro-tip used by every experienced Linux user. Instead of typing history, press:
Ctrl + R
Then start typing a few letters of the command you remember. Bash will search backward through your history and autocomplete it for you. Press Enter to run it or Right Arrow to edit it.
5. Pro-Tips
- Where is it stored? Your history is usually saved in a hidden file in your home directory called
.bash_history(or.zsh_historyif you use Zsh). - Ignoring Duplicates: If you don't want your history filled with the same
lscommand over and over, you can addHISTCONTROL=ignoredupsto your.bashrc. - Timestamping: You can configure history to show the date and time each command was run by setting the
HISTTIMEFORMATvariable. - Arch Linux Context: This is life-saving when you are troubleshooting. If your system breaks after a configuration change, you can run
historyto see exactly what you changed in/etc/or what packages you recently installed.
6. Summary Reference
| Goal | Action |
|---|---|
| View all history | history |
| Search history | Ctrl + R |
| Repeat last command | !! |
| Repeat command #42 | !42 |
| Delete history | history -c && history -w |