DEV Community

Aryan Vaishnani
Aryan Vaishnani

Posted on

Command History & Completion

Linux shells like Bash and Z shell provide powerful features for:

  1. Command history
  2. Auto-completion
  3. Faster command execution
  4. Productivity improvement

1. Command History

What is Command History?

Linux stores previously executed commands in a history file.

This allows users to:

  1. Reuse commands
  2. Avoid retyping
  3. Track previous operations
  4. Speed up administration work

View Command History

Basic Command

History

Run Previous Commands

Run Specific History Command

!4

Executes command number 4.

Repeat Last Command

!!

Repeat Last Command Starting with Specific Text

!docker

Runs latest command starting with docker.

History Navigation

Use keyboard:

Key Purpose
↑ Previous command
↓ Next command

Very commonly used in daily Linux work.

Search History Interactively

Reverse Search

Press:

CTRL + R

Then type:

docker

Searches previous docker commands.

History File Location

For Bash:

~/.bash_history

For Zsh:

~/.zsh_history

Configure History Size

Temporary

HISTSIZE=5000

Permanent

Edit:

nano ~/.bashrc

Add:

export HISTSIZE=10000
export HISTFILESIZE=20000

Reload:

source ~/.bashrc

Ignore Duplicate Commands

Add in .bashrc:

export HISTCONTROL=ignoredups

Clear History

Current Session

history -c

Delete History File

rm ~/.bash_history

Real-World Usage of History

Kubernetes

history | grep kubectl

Find old Kubernetes commands.

Docker

history | grep docker

Troubleshooting

history | grep nginx

Check previous server changes.

2. Command Completion

What is Auto-Completion?

Auto-completion automatically completes:

  1. Commands
  2. File names
  3. Directories
  4. Variables
  5. Kubernetes resources

Mainly done using:

TAB key

Basic Completion Example

Type:

cd Doc

Press:

TAB

Becomes:

cd Documents/

Bash Completion Package

Advanced completion support package.

Install:

Ubuntu/Debian

sudo apt install bash-completion -y

Enable Bash Completion

Edit:

nano ~/.bashrc

Add:

source /usr/share/bash-completion/bash_completion

Reload:

source ~/.bashrc

Top comments (0)