How to Use the Linux history Command

March 3, 2022

Introduction

The history command in Linux is a built-in shell tool that displays a list of commands used in the terminal session. history allows users to reuse any listed command without retyping it.

In this tutorial, we will show you how the history command works and different ways to use it.

How to use the Linux history command

Prerequisites

  • A system running Linux.
  • An account with sudo privileges.
  • Access to the terminal window.

How to Use Linux history Command

Using the history command without options displays the list of commands used since the start of the terminal session:

history
Using the history command to display the command history list

To display the command history list with a limited number of entries, append that number to the history command. For instance, to show only the latest five entries, use:

history 5
Using the history command to display a limited number of command history list entries

Once you close the terminal, the Bash shell saves new command history entries in the .bash_history file.

Use Date and Timestamps

The .bashrc file stores the Bash shell settings. Modifying this file allows you to change the output format of the history command.

Open the .bashrc file using a text editor such as Nano:

sudo nano .bashrc

To change the output format to include date and timestamps, add the following line to the .bashrc file:

export HISTTIMEFORMAT="%c "
Edit the .bashrc file to include timestamps in the history command output

Note: The blank space before the closed quotation marks prevents the timestamp from connecting to the command name, making the history list easier to read.

Using different arguments after HISTTIMEFORMAT allows you to customize the level of detail in the timestamp:

  • %d: Day
  • %m: Month
  • %y: Year
  • %H: Hour
  • %M: Minutes
  • %S: Seconds
  • %F: Full date (Y-M-D format)
  • %T: Time (H:M:S format)
  • %c: Complete date and timestamp (Day-D-M-Y H:M:S format)

Save the changes to the .bashrc file, relaunch the terminal, and run the history command to confirm the new output format:

history
The history command output with timestamps

View the Size of the History Buffer

The .bashrc file contains two entries that control the size of the history buffer:

  • HISTSIZE: The maximum number of entries for the history list.
  • HISTFILESIZE: The maximum number of entries in the .bash_history file.
Entries defining the history buffer size

Editing the HISTSIZE and HISTFILESIZE values changes how the Bash shell displays and saves the command history.

For instance, changing the HISTSIZE value to 10 makes the history command list show a maximum of 10 latest entries.

Editing the history buffer size

Saving the changes to the .bashrc file, relaunching the terminal, and running the history command confirms the new output format:

history
The history command list with 10 entries

Repeat a Command

Running the history command allows you to reuse any of the commands on the list. For instance, to run the first command (sudo apt update) again, use:

!1
Reusing the first command on the history list

Adding a dash (-) before the command number starts the count from the end of the list. For instance, to reuse the tenth last command (history 5), use:

!-10
Reusing a command from the history list, counting from the end

Use double exclamation points to repeat the last command:

!!
Reusing the last command

Search a Command by String

Adding a string after the exclamation point runs the latest command that starts with that string. For example, to reuse the latest command that begins with sudo, use:

!sudo
Reusing a command starting with the "sudo" string

Using this method can cause problems if the shell runs an unexpected command, especially when searching for a command that starts with sudo. As a precaution, adding the :p argument displays the command without running it, allowing you to review the command and decide if you want to execute it.

!sudo:p
Displaying the last command starting with the "sudo" string without executing it

To search for a command that contains a string, but may not start with it, add a question mark next to the exclamation point. For instance, to reuse the last command that contains echo:

!?echo
Reusing the last command that contains the "echo" string

In the example above, the shell reuses the last command that contains the echo string even though the command starts with sudo.

Check out our article on sudo command to learn how to use the sudo command with examples.

List the Matching Commands

Combining history and grep allows you to display a list of commands that contain a string. For example, to list all commands that contain ufw, use:

history | grep ufw
Searching the command history using the history and grep commands

Note: Learn more about using the grep command in Linux.

Change the Executed Command

Use the following syntax to change the last executed command:

^[old string]^[new string]^

For instance, the ufw command to enable port 20 shows that the port is already enabled:

sudo ufw allow 20/tcp
Unsuccessfully running the ufw command

Use the syntax above to change the port number from 20 to 22:

^20^22^
Changing the previous command

Prevent Recording Commands in History

To prevent recording commands in the history list, temporarily disable recording by using:

set +o history

To re-enable recording, use:

set -o history

Delete History

Use the -d option with the history command to delete a command from the history list. For instance, delete command number 87 with:

history -d 87
Deleting an entry from the command history list

Use the -c option to clear the whole history list:

history -c

Update the History File

The Bash shell saves any updates to the command history list when you exit the terminal session. The history command also allows you to save changes while in the terminal session.

Using the -a option lets you append the command history entries from this session to the .bash_history file:

history -a

Another method is to use the -w option to save the entire history list to the .bash_history file:

history -w

Conclusion

After reading this tutorial, you should be able to use the history command in Linux to view, edit, and delete the command history list and reuse commands from it.

If you are interested in learning more about Linux commands, have a look at our Linux commands cheat sheet.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How to Use the ulimit Linux Command
December 9, 2021

The ulimit shell command allows you to view or limit the amount of resources a user can consume. Limiting resources prevents adverse effects in the system when a user or program consumes all the available resources.
Read more
How to Use the Linux at Command
February 2, 2022

The at command allows you to schedule a job for one-time execution. This guide shows how to install and use the at command, with several useful examples.
Read more
How to Use the Linux head Command
January 5, 2022

The head commands lists the first ten lines of a file in standard output. Learn how to use the head command and its options (with examples).
Read more
How to Use the Linux diff Command
December 29, 2021

Use the diff command to compare files line-by-line and see how to modify them to make them identical.
Read more