Linux history Command with Examples

May 30, 2024

Introduction

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

This tutorial explains how the history command works and different ways to use it.

How to Use the Linux history Command

Prerequisites

history Command Syntax

The history command syntax is:

history [options]

The command can be run without any options, but they provide useful functionalities.

history Command Options

The history command works with different options that change its output. The table below presents commonly used history options:

OptionDescription
-cClears the entire history list.
-d offsetAllows you to delete a specific entry from the command history. The offset is the position number of the command in the history list you want to remove.
-aAppends the new history lines (those entered since the beginning of the current session) to the history file.
-nReads all history lines not already read from the history file into the current session.
-rReads the history file and appends its contents to the current history list.
-wWrites the current history list to the history file.
-pPerforms history expansion on each argument and displays the result without storing it in the history list. This option allows you to see how the history expansions would be interpreted without adding them to the history list.
-sStores the arguments in the history list as a single entry.

history Command Examples

The history command is used to view and manage previously executed commands in the terminal, making it easier to repeat past commands. The following text presents history use case examples.

List Commands Used in the Terminal Session

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

history
history terminal output

Limit the Number of Entries

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
history 5 terminal output

Search Commands in History

To find specific commands in the command history, pipe history with the grep command. For example, find the ls command with:

history | grep "ls"
history | grep "ls" terminal output

Use Date and Timestamps

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

To change these settings, open the .bashrc file using a text editor such as Nano:

sudo nano .bashrc

Change the output format to include the date and timestamps by adding the following line to the .bashrc file:

export HISTTIMEFORMAT="%c "
add date and timestamps to the history 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
history with date and time terminal output

View the Size of the History Buffer

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

  • HISTSIZE. The maximum number of entries for the history list.
  • HISTFILESIZE. The maximum number of entries in the .bash_history file, the file that stores the history command list.
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

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

history
history last ten entries terminal output

Repeat a Command

The history command prints a list of previously used commands. To reuse any command from that list, use the exclamation point (!) and the number of the command. For example, run the third command from a list with:

!3
!3 terminal output

Add a dash (-) before the command number to run a specific command from the end of the list. For instance, to reuse the seventh last command, use:

!-7
!-7 terminal output

Use double exclamation points to repeat the last command:

!!
!! terminal output

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
!sudo terminal output

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

!sudo:p
!sudo:p terminal output

To search for a command that contains a string but doesn't start with it, add a question mark next to the exclamation point. For instance, to reuse the last command that contains ls, enter:

!?ls
terminal output for !?ls

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

Change the Executed Command

If you made a typo in a previous command, use the following syntax to fix it:

^[old string]^[new string]^

For instance, the sudo atp update command is misspelled:

sudo atp update terminal output

Use the syntax above to change atp into apt:

^atp^apt^
changing atp into apt terminal output

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

The commands have no output

Delete History

Use the -d option with the history command to delete a command from the history list. For instance, delete the command with the number 34 (located by running history 5).

history -d 34
history -d 34 terminal output

The command has no output. Verify the change by running history 10:

history 10
history 10 terminal output

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 manually while in the terminal session.

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

history -a

The command has no output. Another method is to use the -w option to save the entire history list to the .bashrc_history file:

history -w

This command also doesn't have any output.

Conclusion

This tutorial explained how to use the history command in Linux to view, edit, and delete the command history list and reuse commands from it.

To learn more about Linux commands, check out our Linux commands cheat sheet.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
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
July 18, 2024

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