In Linux, the “history” command is used to view a list of previously executed commands in the current session. It provides a convenient way to recall and reuse commands without typing them again. Here’s how to use the “history” command:
- Open a Terminal:
Launch a terminal emulator on your Linux system.
- Type the “history” command:
Simply type “history” and press Enter. For example:
- View Command History:
The “history” command will display a numbered list of previously executed commands, with the most recent commands appearing at the bottom of the list. Each command is associated with a unique number, allowing you to reference it for further use.
- Use Command History:
To reuse a command from the history, you have a few options:
- Type the command number followed by an exclamation mark (!) and press Enter. For example, to rerun command number 123, type:
- Use the Up and Down arrow keys to navigate through the command history. Press Enter to execute the selected command.
- Press Ctrl+R and start typing a portion of a previously executed command. The terminal will search the history for a match, allowing you to quickly retrieve and execute the desired command.
- Limit the Number of History Entries:
By default, the “history” command displays the entire command history for the current session. However, you can limit the number of history entries displayed by providing a number as an argument. For example, to view the last 10 commands, run:
- Save Command History:
The command history is typically saved to a file, such as ~/.bash_history, allowing you to retain command history across different terminal sessions. The history file is usually automatically updated when you close the terminal.
- Exiting the Command:
The “history” command will display the command history and return you to the command prompt. You can continue executing other commands or exit the terminal as needed.
The “history” command is a useful tool for recalling and reusing previously executed commands. It can save you time by avoiding retyping long or complex commands and helps you keep track of your command history within a session.