🐧 How to Use the history Command in Linux (Step-by-Step Guide)

🐧 How to Use the history Command in Linux (Step-by-Step Guide)
Operating System(s)

The history command in Linux lets you view, search, and reuse previously executed commands. It’s a huge time-saver for developers, sysadmins, and anyone working in the terminal.

🔎 What is history?

history displays a list of commands you’ve run in your shell session.

👉 Each command is stored with a number
👉 You can quickly rerun or search past commands

✅ Basic Usage

history

👉 Shows a numbered list of recent commands

🔁 Run a Previous Command

!100

👉 Runs command number 100 from history

🔄 Run the Last Command Again

!!

👉 Repeats the most recent command

🔍 Search Command History

Search with grep

history | grep ssh

👉 Finds all commands containing ssh

Reverse Search (Best Method)

Press:

Ctrl + R

Then type a keyword 👉 it searches interactively

🧹 Clear History

history -c

👉 Clears current session history

💾 Save History Manually

history -w

👉 Writes history to file

📂 Where History is Stored

Most shells store history in:

~/.bash_history

⚙️ Useful Options

CommandDescription
history 10Show last 10 commands
history -d 100Delete command #100
history -cClear history
history -aAppend new commands to file

🚀 Real-World Examples

Re-run last sudo command

sudo !!

Find a previous install command

history | grep apt

Quickly repeat a long command

!ssh

👉 Runs last command starting with ssh

⚠️ Important Notes

  • History may not save immediately unless configured
  • Sensitive commands (passwords) may be stored
  • Clearing history doesn’t always erase system logs

🧠 Pro Tips

  • Use Ctrl + R instead of scrolling
  • Increase history size:

    export HISTSIZE=5000
    export HISTFILESIZE=10000

  • Avoid saving sensitive commands:

    export HISTCONTROL=ignoreboth

👍 Quick Summary

  • history shows past commands
  • Use !number to rerun commands
  • Use Ctrl + R to search instantly
  • Stored in ~/.bash_history

🎯 Final Thoughts

The history command is one of the fastest ways to work smarter in Linux. Once you start using reverse search and command recall, you’ll rarely need to type long commands again.

Force Cache (Faster Mirror)

Linux Commands

Files

tar · pv · cat · tac · chmod · grep ·  diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr · hash  · compgen

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami ·· iptables · ssh-keygen · ufw · arping · firewalld