Skip to content

Command History

Every command you execute in the Linux shell is recorded in your command history. Learning how to view, search, reuse, and manage command history can dramatically improve your productivity and help you work faster on Linux systems.


Learning Path

Linux Mastery → Module 2: Linux Command Line Essentials → Lesson 8

Difficulty: Beginner

Reading Time: 20 Minutes

Course Progress

Course: Linux Mastery

Module: Linux Command Line Essentials

Lesson: 8 of 10


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand how Bash stores command history
  • View and search previous commands
  • Re-execute commands from history
  • Customize history behavior
  • Clear and manage history safely
  • Improve productivity using history shortcuts

Prerequisites

Before starting this lesson, complete:

  • Module 1 – Linux Fundamentals
  • Module 2 Lessons 1–7

Why Learn Command History?

Imagine you previously executed a complex command like:

kubectl get pods --all-namespaces --sort-by=.metadata.creationTimestamp

Instead of typing it again, Linux allows you to retrieve it instantly.

Command history helps you:

  • Save time
  • Reduce typing errors
  • Repeat complex commands
  • Troubleshoot efficiently
  • Learn from previously executed commands

What is Command History?

Bash automatically records the commands you execute during a session.

Example:

pwd

ls -la

cd /etc

cat hosts

history

Each command is assigned a unique history number.


Display Command History

history

Example output:

1 pwd

2 ls -la

3 cd /etc

4 cat hosts

5 history

Display Last Few Commands

Display the last five commands:

history 5

Example:

96 ls

97 pwd

98 whoami

99 date

100 history

Execute Previous Commands

Run a command by its history number.

Example:

!98

Executes command number 98.


Execute the Last Command

Run the previous command again.

!!

Example:

sudo !!

Scenario:

apt update

Permission denied.

Instead of typing again:

sudo !!

Bash automatically executes:

sudo apt update

This is one of the most useful Bash shortcuts.


Execute Commands by Prefix

Run the most recent command starting with:

!git

If your previous command was:

git status

It runs automatically.

Other examples:

!kubectl

!docker

!terraform

Search Command History

One of Bash's most powerful features.

Press:

Ctrl + R

Example:

Search:

docker

Output:

(reverse-i-search)`docker':
docker ps -a

Press:

  • Ctrl + R again for older matches
  • Enter to execute
  • Esc to edit before execution

Search Using grep

Display commands containing a keyword.

history | grep docker

Example:

15 docker ps

18 docker images

20 docker logs

Another example:

history | grep kubectl

History File

Bash stores history in:

~/.bash_history

View it:

cat ~/.bash_history

History Variables

Display history size.

echo $HISTSIZE

Example:

1000

Display history file size.

echo $HISTFILESIZE

Example:

2000

View history file location.

echo $HISTFILE

Output:

/home/basha/.bash_history

Customize History Size

Temporary change:

HISTSIZE=5000

Persistent change:

Edit:

nano ~/.bashrc

Add:

HISTSIZE=5000

HISTFILESIZE=10000

Apply changes:

source ~/.bashrc

Ignore Duplicate Commands

Prevent consecutive duplicate commands.

export HISTCONTROL=ignoredups

Ignore duplicates and commands beginning with a space.

export HISTCONTROL=ignoreboth

Ignore Specific Commands

Example:

export HISTIGNORE="ls:pwd:history"

These commands won't be saved.


Clear Command History

Clear the current session.

history -c

Clear and save.

history -c

history -w

Delete a Specific Entry

Delete history entry number 45.

history -d 45

Append Current Session

Write current session to history file.

history -a

Read history file.

history -r

Rewrite history file.

history -w

Useful Keyboard Shortcuts

Shortcut Purpose
Previous command
Next command
Ctrl + R Reverse search
Ctrl + P Previous command
Ctrl + N Next command
!! Previous command
!number Execute command by number
!string Execute last command starting with string

Command Summary

Command Purpose
history Display history
history 10 Last 10 commands
!! Previous command
!25 Execute command 25
!git Execute last git command
history -c Clear history
history -d Delete entry
history | grep Search history

Production Perspective

Command history is extremely useful for:

  • Recovering previously used commands
  • Debugging production issues
  • Repeating deployment commands
  • Auditing troubleshooting steps
  • Learning from experienced administrators

Example:

history | grep kubectl

history | grep docker

history | grep terraform

history | grep ansible

During an incident, engineers often search history to reuse working commands quickly.


Hands-on Lab

Task 1

View command history.

history

Task 2

Display the last 10 commands.

history 10

Task 3

Search for ls.

history | grep ls

Task 4

Run the previous command.

!!

Task 5

Display history file.

cat ~/.bash_history

Task 6

Display history variables.

echo $HISTSIZE

echo $HISTFILESIZE

Task 7

Try reverse search.

Ctrl + R

Search for:

history

Command Deep Dive

Command Purpose Common Options Production Example
history Display command history -c, -d, -a, -w Review previous troubleshooting steps
!! Execute previous command Default Retry failed command with sudo
!number Execute by history number Default Repeat deployment command
Ctrl + R Search history Interactive Find previous kubectl commands

Production Troubleshooting Scenario

Scenario

An application stopped responding after a deployment.

You need to determine what commands were executed earlier.

Tasks:

  1. Display recent history.
  2. Search for kubectl commands.
  3. Search for docker commands.
  4. Re-run the last systemctl command.
  5. Save the current session history.

Example:

history

history | grep kubectl

history | grep docker

!systemctl

history -a

Mini Challenge

Perform the following:

  1. Execute at least 10 Linux commands.
  2. Display your command history.
  3. Search for ls.
  4. Search for cd.
  5. Execute the last pwd command using history.
  6. Display your history file.
  7. Delete one history entry.
  8. Clear the current session history.

Best Practices

  • Use Ctrl + R instead of retyping long commands.
  • Increase HISTSIZE for larger command history.
  • Ignore duplicate commands to keep history clean.
  • Avoid storing sensitive commands containing passwords.
  • Review history during troubleshooting to retrace your steps.

Common Mistakes

❌ Re-typing long commands repeatedly.

✅ Use:

Ctrl + R

❌ Clearing history without understanding the consequences.

✅ Use:

history -c

This removes the current session history.


❌ Assuming history is shared across all users.

✅ Each user has their own history file.


Interview Questions

Beginner

  1. What command displays Bash history?
  2. What does !! do?
  3. How do you search command history?
  4. Where is Bash history stored?

Intermediate

  1. Explain HISTSIZE and HISTFILESIZE.
  2. How do you ignore duplicate commands?
  3. What is the purpose of history -a and history -w?
  4. How do you execute a command by its history number?

Architect Level

  1. How can command history improve incident response?
  2. What are the security considerations when storing command history?
  3. How would you configure history for administrators on production servers?

Summary

In this lesson, you learned:

  • Viewing command history
  • Searching history
  • Re-executing previous commands
  • Managing history files
  • Customizing history behavior
  • Productivity shortcuts

Command history is one of Bash's most valuable features. Mastering it will help you work faster, avoid repetitive typing, and troubleshoot systems more efficiently.


Key Takeaways

  • history displays previously executed commands.
  • Ctrl + R is the fastest way to search command history.
  • !! repeats the last command.
  • !number executes a command by its history number.
  • HISTSIZE and HISTFILESIZE control history storage.
  • Command history can significantly improve productivity and troubleshooting.

What's Next?

Input, Output, Pipes, and Redirection

In the next lesson, you'll learn:

  • Standard Input (stdin)
  • Standard Output (stdout)
  • Standard Error (stderr)
  • Output redirection (>, >>)
  • Input redirection (<)
  • Pipes (|)
  • The tee command
  • Building powerful Linux command pipelines