history Command in Linux



The history command is a valuable utility that you can use to manage your command-line activities. This command is especially useful in the Bash shell, where it helps you view, navigate, and manipulate the list of commands you previously executed.

  • Each command is logged as an event with a unique identifier, making it easy to recall and reuse. When you have long and complex commands, it's easy to make mistakes when typing them out again. By using the history command, you can avoid these errors.
  • While you can use the Up arrow key to scroll through previous commands, the history command offers more powerful ways to search and reuse commands. For example, you can search for a specific command by typing history | grep <keyword>

When you run commands in the terminal, they are temporarily stored in the shell's memory. These commands are not immediately written to the history file. Instead, they are appended to the history file (like ~/.bash_history for Bash or ~/.zsh_history for Zsh) when the terminal session ends. It that the history file is updated with the latest commands from your session. If you need to save the current session's commands to the history file without closing the terminal, you can use the "-a" flag with the history command.

The history command provides several useful options. For example,

  • You can display a specific number of recent commands by specifying a number, such as history 10 to show the last 10 commands.
  • You can also search through your command history using grep, which lets you quickly find and reuse commands matching a specific pattern. One of the most powerful features of the history command is the ability to recall and execute previous commands using their event numbers.
  • By prefixing an event number with an exclamation mark (!), you can re-execute that specific command. For instance, !37 would re-run the command associated with event number 37. This feature enhances productivity by reducing the need to retype complex commands.

Table of Contents

Here is a comprehensive guide to the options available with the history command −

Syntax for the history Command

The following is the basic syntax of the history command −

history [option]

Options Available for the history Command

The following table highlights the tags commonly used with the history command –

Tag Description
-c Clears the entire command history.
-d Deletes the history entry at a specific position offset.
-a Appends the current session to the history file
-n Reads all history lines not already read from the history file into the current session's history
-r Reads the history file and appends its contents to the current session's history.
-w Replaces the content of the history file with your current session history.
-p arg [arg ...] Performs history substitution on the following arguments and displays the result. It doesn't save the result to the history list.
-s Appends the arguments to the history list as a single entry.

Examples of history Command in Linux

In this section, we'll explore various practical examples and useful hacks of the history command.

View All Previously Executed Commands

To list all the commands you've executed in the current session, you can simply run the following command −

history

As you can see, the commands are numbered, with the most recently executed (those with the highest numbers) at the end of the list.

history Command in Linux1

To view a certain number of commands, you can pass a number to the history command. For instance, to view the last 5 commands you've used, run the following command −

history 5
history Command in Linux2

You can also achieve the same output if you pipe history through the tail command −

history | tail -n 5
history Command in Linux3

Repeat a Specific Command

To reuse a command from the history list, you can simply type an exclamation point (!), and the number of the command as shown −

!830
history Command in Linux4

Repeat the Last Command

To repeat the last command you executed, you can type two exclamation marks with no spaces −

!!
history Command in Linux5

This can be useful when you issue a command and forget to use sudo. Simply type sudo, one space, the double exclamation mark, and then hit Enter. Suppose the last command you ran was trying to install a package. To repeat it with sudo, you can use the following syntax −

sudo !!
history Command in Linux6

If you also want to execute any command from the history list, you can use one exclamation mark, a hyphen (-), and the number of any previous command to execute it.

For instance, if you want to execute the 875 previous command, you can run −

!-875
history Command in Linux7

Search for a Command

You can use the history | grep "keyword" command to search through your command history for any commands that contain the specified keyword.

For example, if you want to find all the commands in your history that include the word "sudo," you can run −

history | grep "sudo"

This displays all the commands that contain “sudo” from the history list.

history Command in Linux8

Clear the History

To clear the entire command history for the current session, you can use the “-c” option with the history command −

history -c
history Command in Linux9

Append Command to History

To add a command to the history list, you can simply use the following syntax −

history -s 'echo "Hello, World!"'
history Command in Linux10

Write Session History to File

To save the current session’s history to the history file, you can use the “-w” flag with the history command −

history -w
history Command in Linux11

Delete Specific Entry

If you want to remove a specific command from your history without clearing the entire session, you can use the “-d” option with the history command −

history -d 890
history Command in Linux12

Append Current Session to History File

To append the current session’s commands to the history file, you can simply run the history command with the “-a” option −

history -a

This ensures that any commands you’ve executed in the current session are saved to the history file immediately, rather than waiting until the session ends.

history Command in Linux13

Add Timestamps to History

To add timestamps to your command history, you can use the HISTTIMEFORMAT environment variable −

export HISTTIMEFORMAT="%F %T "

Where −

  • %F will display the date in YYYY-MM-DD format.
  • %T will display the time in HH:MM:SS format.

Now to view your history with timestamps, you can run:

history

This adds date and time to each command in your history, making it easier to track when commands were executed.

history Command in Linux14

Ignore Specific Commands

You can use the HISTIGNORE environment variable to specify a list of commands that you don’t want to be saved in your command history. For example −

export HISTIGNORE="ls:cd:pwd"

This command ignores ls, cd, and pwd commands, meaning they won’t be recorded in your history. You can add more commands to this list by separating them with colons.

history Command in Linux15

Conclusion

The history command in Linux is an essential tool for efficient command-line management. By allowing you to view, navigate, and manipulate previously executed commands, it enhances productivity and minimizes the risk of errors associated with retyping complex commands.

Mastering the history command and its various options not only saves time but also fosters a more efficient and error-free command-line experience. By integrating these practices into your workflow, you can navigate the Linux space with greater ease and confidence.

Advertisements