12. history

history Command in Linux

The history command shows a list of all previously run commands in the terminal.
It’s very useful to recall or repeat past commands.


🔹 Syntax:

history

🔹 Example:

history

👉 Output:

  1  ls
  2  pwd
  3  cd /home/ashwin
  4  mkdir test
  5  history

This shows:

  • Line numbers for each command

  • The exact command you typed earlier


🔹 Useful Options & Examples:

1. Run a previous command by number

!3

👉 Runs the 3rd command from the history (in this case: cd /home/ashwin)


2. Search your command history

history | grep mkdir

👉 Finds all mkdir commands you've used before.


3. Repeat the last command

!!

👉 Runs the most recent command again.


4. Clear your command history

history -c

👉 Clears all saved history for the current session.


5. Save history to a file

history > my_history.txt

👉 Saves your full command history into my_history.txt.

Try with Linux Practice