To see the last command you executed in Linux, use the built-in command history feature. No extra setup is required—just run a simple command in your terminal. The history is stored in a file (usually ~/.bash_history for Bash) and can be viewed interactively or programmatically. For persistent access, configure your shell to display history automatically when you open a terminal.
What You'll Need
- The Linux terminal (default on most Linux distros; available via
Ctrl+Alt+Tin GNOME/KDE orTerminalapp in others). - A shell that supports command history (Bash, Zsh, Fish, etc.). Most modern Linux systems use Bash by default.
- Basic terminal knowledge (open a shell, type commands, and press
Enter).
Step-by-Step
-
Open your terminal. Press
Ctrl+Alt+T(GNOME/KDE) or launch the Terminal app (Xfce, LXQt, etc.). If you’re unsure which shell you use, runecho $SHELLto check (common shells: Bash, Zsh, Fish). -
View the last command interactively. Run one of these commands to display your most recent history:
history: Lists all commands in reverse chronological order (last command first). Pressqto exit.fc -l: Shows a numbered list of your last commands (e.g.,fc -l 10to see the last 10).history | head -n 1: Displays only the last entry.
-
Access the last command directly. Use the shell’s history navigation:
!!: Repeats the last command entered.!#(e.g.,!10): Repeats the command entered at position #10 in the history.Ctrl+R: Press repeatedly to search and rerun commands from history (type a partial command to filter).
-
View history in a file (advanced). The full history is saved in
~/.bash_history(create it if missing withtouch ~/.bash_history). To edit it:- Open the file with a text editor:
nano ~/.bash_history(typeCtrl+Oto save,Ctrl+Xto exit). - Look for your last command near the top of the file.
- Open the file with a text editor:
Common Mistakes
-
Shell doesn’t recognize history commands. If
historyor!!fail, your shell may not be recording history. Check if history is enabled by running:echo $HISTFILESIZEIf it returns0or lacks a value, history is disabled. Enable it in your shell config file (e.g.,~/.bashrcor~/.zshrc) by adding:HISTFILESIZE=10000HISTSIZE=10000Then reload withsource ~/.bashrcor restart the terminal. -
History shows old commands. If you’re unsure what’s in history, clear it with:
history -c(clears the current session) orrm ~/.bash_history(clears permanent history). -
Permissions issue. If you can’t read/write
~/.bash_history, add your user to thewheelgroup (Linux) or use sudo to edit:sudo nano /root/.bash_history
Tips
-
Make history persistent. To see history every time you open a terminal, add this to your shell config file:
if [ -f ~/.bash_history ]; then source ~/.bash_history; fiThis ensures history loads automatically. -
Filter history. Use
grepto search for commands:history | grep "command"Example:history | grep "ls"lists alllscommands. -
Save history to a file. Redirect history to a file for later review:
history > my_history.txt -
Use a different shell. If Bash isn’t your default, switch to Zsh (Ubuntu/Debian):
sudo apt install zshchsh -s $(which zsh)Then follow the initial setup prompts.
Troubleshooting
If the last command isn’t visible, try these steps:
-
Restart your shell. Close and reopen the terminal. If you’re in an interactive session (e.g., SSH), restart with
exec bash. -
Check shell settings. Run:
echo $HISTSIZE $HISTFILESIZEIf these values are low, increase them in your shell config (e.g.,HISTSIZE=10000). -
Verify history file exists. Run:
ls -la ~/.bash_historyIf it’s missing, create it:touch ~/.bash_historyThen add commands to see it populate. - Test with a minimal setup. Open a new terminal and type a command manually. If history appears, the issue is with your existing session.
-
Reinstall the shell. If nothing works, reinstall Bash:
sudo apt reinstall bash(For Debian/Ubuntu) orsudo dnf reinstall bash(For Fedora/RHEL).
Frequently Asked Questions
Why isn’t my last command showing up in history?
History may be disabled or empty if you haven’t run any commands since starting the session. Enable it permanently by adding HISTSIZE=10000 to your shell config file.
How do I clear the history without deleting my commands permanently?
Use history -c to clear the current session’s history or rm ~/.bash_history to clear it permanently.
Can I see the last command in a non-interactive shell (e.g., scripts)?
Yes, but history won’t be available. To capture the last command in a script, use echo $BASH_COMMAND or echo $LAST_COMMAND.
What if my shell isn’t Bash (e.g., Zsh, Fish)?
Zsh uses history similarly but may have additional options like fc -l. Fish stores history in ~/.config/fish/fish_history.
How do I prevent sensitive commands from appearing in history?
Use a command alias with set +x to hide output while typing:
alias mycmd="set +x; command mycmd"
Then type mycmd instead of command mycmd.
Can I search for a specific command in my history?
Use history | grep "search_term" or fc -l | grep "search_term" to filter results.