Skip to content

Introduction to the history Command in Linux

The history command in Linux is used to display the list of commands that have been entered in the terminal. It is a useful tool for reviewing, recalling, and reusing previous commands. In the ChromeOS Linux Environment (Crostini), history helps streamline workflows by allowing users to reference past commands efficiently.

Syntax and Usage

The basic syntax of the history command is as follows:

history [options] [n]
  • n: The number of recent commands to display.
  • options: Flags to modify the behavior of the history command.

Common Use Cases

  1. Display the entire command history:

    history
    
  2. Display the last N commands:

    history 10
    
  3. Search command history interactively (using Ctrl+R):

    Press Ctrl+R and type a part of the command to search backward through your history.

  4. Execute a specific command from history by its number:

    !123
    

    This executes the command associated with entry number 123.

  5. Clear the command history:

    history -c
    
  6. Delete a specific entry from the history:

    history -d 123
    

Useful Options

  • -c: Clear the history.
  • -d [offset]: Delete the history entry at the specified offset.
  • -w: Write the current session’s history to the history file.
  • -r: Read the history file and append its contents to the current session's history.

Persistent History

The command history is typically stored in a file (e.g., ~/.bash_history for Bash). To ensure your command history is saved between sessions:

  1. Write the current session's history to the file:

    history -w
    
  2. Read the history file into the current session:

    history -r
    

Special Notes for ChromeOS Linux Environment

In ChromeOS Linux, the history command functions as it does in other Linux distributions. However, given the shared nature of the Crostini environment, command history might be particularly helpful for managing repetitive tasks or troubleshooting system configurations.

For example, recalling installation commands or debugging scripts:

history | grep "apt install"

Conclusion

The history command is a powerful tool for managing and recalling past terminal commands. Its features improve efficiency and reduce the need to retype lengthy or complex commands, making it an essential utility in the ChromeOS Linux Environment.