history

history

The history command in Unix and Linux is used to display the list of previously executed commands in the terminal session. This is particularly useful for recalling commands without having to retype them, and for searching through your command history to find specific commands you ran previously.

Basic Usage

The syntax for the history command is:

history [options] [n]
  • n: Display the last n commands.

When used without any options or arguments, history will display all commands in the history list.

Examples

Displaying Command History

To display the complete command history:

history

This will output a numbered list of commands you have executed in the current terminal session:

1  ls
2  cd /var
3  cat /etc/passwd
4  history

Displaying the Last N Commands

To display the last 5 commands, for example:

This will output the last 5 commands:

Using History for Command Recall

Re-executing Commands

You can re-execute a command from the history list using the exclamation mark (!) followed by the command number. For example, to re-execute command number 12:

This will re-run mkdir test.

Repeating the Last Command

To repeat the last command you executed:

This will re-run the most recent command.

Using History Search

You can search through your command history using Ctrl + r. This will start a reverse incremental search. Start typing the command you are looking for, and it will appear:

Press Enter to execute the command.

History Substitution

Running a Command with Modifications

You can re-run a previous command with modifications. For example, if you previously ran:

You can re-run it with a different directory:

This will execute:

Options

Clearing the History

To clear the history list:

This will clear the history of the current session.

Appending to the History File

By default, the history command saves the history of each session in a file specified by the HISTFILE environment variable (usually ~/.bash_history). To append the current session’s history to this file:

Configuration

You can customize the behavior of the command history using environment variables:

  • HISTSIZE: The number of commands to remember in the current session.

  • HISTFILESIZE: The maximum number of lines contained in the history file.

  • HISTIGNORE: A colon-separated list of patterns used to decide which command lines should be saved in the history list.

For example, to set the history size to 500 commands and to ignore commands starting with ls:

Practical Use Cases

Auditing Commands

The history command is useful for auditing the commands that were run on a system. By checking the history, you can see what actions were performed in previous sessions.

Scripting and Automation

When creating scripts, it is helpful to recall and reuse complex commands you’ve run before. The history command makes it easy to find and repeat those commands.

Conclusion

The history command is a powerful tool for managing and recalling the commands you’ve executed in Unix and Linux systems. With its various options and the ability to re-execute, modify, and search through past commands, it greatly enhances the efficiency of working in the terminal. By understanding and utilizing the history command, you can save time and avoid repetitive typing. If you have any further questions or need assistance with anything else, feel free to ask!a

help

Last updated