Linux Basic Commands

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