Linux History Command: How to Use It & What It Does
In this article
The Linux history command shows a numbered list of commands you've typed in the terminal, lets you re-run them quickly, and helps you avoid retyping long or complex commands. You can search, edit, and even clear your history. No special tools are needed—it's built into most Linux shells (Bash, Zsh, etc.).
What You'll Need
- A Linux system with a terminal (any distribution works).
- Basic familiarity with opening a terminal and typing commands.
- No installation required—
historyis part of the shell.
Step-by-Step
- Open a terminal on your Linux machine. You'll see a shell prompt like
user@host:~$. - Run a few commands to build up history. For example, type
ls,pwd, andecho "hello". Each command is saved automatically. - View your history by typing
historyand pressing Enter. You'll see a numbered list, like:
1 ls
2 pwd
3 echo "hello"
The numbers are line numbers, not command IDs. - Re-run a command by its number using
!number. For example,!2runspwdagain. This is faster than scrolling through your history. - Re-run the last command with
!!. This is handy when you forgotsudo—typesudo !!to run the previous command with elevated privileges. - Search your history by pressing
Ctrl+Rand typing part of a command. The shell shows the most recent match; pressCtrl+Ragain to cycle older matches. Press Enter to run it, or Esc to cancel. - Edit a command from history by pressing the Up arrow to recall the last command, then use Left/Right arrows and Backspace to modify it. Press Enter to run the edited version.
- Clear your history with
history -c. This removes all commands from the current session's history. To also clear the history file (so it doesn't reappear in new sessions), usehistory -wafter-cto overwrite the file, or simplyhistory -c && history -w. - Search for a specific command using
grep. For example,history | grep sshshows all commands containing "ssh". This is useful when you can't remember the exact syntax. - Save your history to a file for backup or review:
history > myhistory.txt. This creates a text file with all commands in the current session.
Common Mistakes
- Assuming history is shared across terminals. By default, each terminal session has its own history. Commands from one terminal won't appear in another until the session ends and the history is written to the file.
- Using
!$incorrectly.!$refers to the last argument of the previous command, not the whole command. For example, afterls /var/log,!$expands to/var/log. - Forgetting that
history -conly clears the current session. If you don't also clear the history file, old commands will reappear in new sessions. - Typing
historyand expecting it to show commands from other users. It only shows your own commands.
Tips
- Use
Ctrl+Rfor reverse search. It's the fastest way to find a command you used yesterday. - Combine
historywithgrepto find commands that used a specific option. For example,history | grep 'grep -r'. - Set a larger history size. By default, Bash stores 500 commands. To increase it, add
export HISTSIZE=10000andexport HISTFILESIZE=20000to your~/.bashrcfile, then runsource ~/.bashrc. - Ignore commands that contain sensitive data. Add
export HISTIGNORE="*password*: *token*"to your~/.bashrcto prevent those from being saved. - Use
history -d line_numberto delete a specific entry. For example,history -d 15removes line 15.
Troubleshooting
If history shows nothing or only a few commands: Your shell may not be saving history properly. Check if you are using a non-interactive shell (like a script) or if the history file is read-only. Try running echo $HISTFILE to see the file location. If it's empty, set HISTFILE=~/.bash_history in your ~/.bashrc.
If Ctrl+R doesn't work: Some terminals or shells have different key bindings. In Zsh, Ctrl+R also works, but if it doesn't, try using fc -r to reverse the history order and then search manually with grep.
If !! runs the wrong command: Make sure you know what the previous command was. You can check with history | tail -5 before using !!.
If history is lost after closing the terminal: This usually happens when the shell exits abnormally. To force saving, run history -w before closing. Also, ensure your ~/.bashrc has shopt -s histappend to append rather than overwrite history.
Frequently Asked Questions
How do I see the last 10 commands?
Type history 10 (the number is the count, not the line range). This shows the last 10 commands.
Can I use history in scripts?
Yes, but by default non-interactive shells don't enable history. You can enable it with set -o history in the script, but it's rarely useful.
How do I prevent a command from being saved in history?
Prefix the command with a space (e.g., ls) if HISTCONTROL=ignorespace is set. You can check with echo $HISTCONTROL. If it's not set, add export HISTCONTROL=ignorespace to your ~/.bashrc.
What is the difference between history and fc -l?
They are similar. fc -l lists history entries with numbers, but fc is more powerful for editing and re-executing commands. For most users, history is simpler.
Can I search history for a command that I partially remember?
Yes, press Ctrl+R and type a keyword. Or use history | grep keyword to see all matches.
How do I clear only a specific range of history?
Use history -d start-end, for example history -d 10-20 deletes lines 10 through 20. This works in Bash 5.0 and later.