Home > Package & System Management > history

Guide to the history Command: Viewing and Managing Shell Command History

The history command displays a list of commands that the user has previously executed in the terminal. This is very useful for easily finding and re-executing commands used in the past or remembering what tasks were performed. Through this guide, learn the basic usage of the history command and various options necessary for managing history.

Overview of history

Every time a user enters a command in the terminal, that command is recorded in a history file (usually `~/.bash_history` or `~/.zsh_history`). The `history` command reads the contents of this file and displays it on the screen, and the history is preserved even after the shell session ends.

Main Roles of history

The history command is primarily used for the following purposes:

Key Use Cases

  • Re-executing commands: You can easily re-execute long commands used previously without needing to retype them.
  • Tracking tasks: It helps to check what tasks were performed in the past and understand the workflow.
  • Improving productivity: Quickly find frequently used commands, saving input time.
  • Reference for script writing: Utilize combinations of commands that were successfully executed in the past for script writing.

Environment Variables and history

The history functionality is influenced by several environment variables:

Key Related Environment Variables

  • HISTSIZE: The maximum number of command history entries to be stored in memory. It is retained during the current shell session.
  • HISTFILESIZE: The maximum number of command history entries to be stored in the history file (e.g., ~/.bash_history). It is preserved between shell sessions.
  • HISTFILE: Specifies the path of the file where command history is stored. The default is ~/.bash_history.
  • HISTCONTROL: Controls the way history records are maintained. (e.g., ignoredups - ignore duplicate commands, ignorespace - ignore commands that start with a space)

Key history Command Options

The `history` command provides various options necessary to query, modify, and manage the history list.

1. Viewing and Outputting History

2. Modifying and Re-executing History

Generated command:

Try combining the commands.

Description:

`history` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn how to effectively query and reuse command history through various examples of the `history` command.

View the Last 20 Commands

history 20

Outputs a list of the 20 most recently executed commands along with their numbers.

Search for Specific String in Command History

history | grep docker

Use the `grep` command to find commands containing 'docker' in the `history` history.

Re-execute Previous ls Command

!ls

Finds and re-executes the most recently executed command starting with `ls`.

Reuse Last Argument of Last Command

cat /var/log/syslog
less !$

Useful when you want to reopen `/var/log/syslog` with the `less` command after executing `cat /var/log/syslog`.

Immediately Save Current Shell History to File

history -w

Immediately save all commands executed in the current session to the `~/.bash_history` file before closing the shell.

Delete Specific History Entry Number

history -d 125

Deletes the command with number 125 from history when checked using the `history` command. (Note: Once deleted, it cannot be recovered)



Same category commands