How to Use the Linux history Command {With Examples}

Introduction

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

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

Prerequisites

  • A system running
  • Linux.

  • An account with sudo privileges
  • . Access to the terminal

  • window.

How

to use the Linux history command When using the history command without options, the list of commands

used since the start of the terminal session is displayed:

history

To display the command history list with a limited number of entries, Add that number to the History command. For example, to display only the last five entries, use:

history 5

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

.

Use date and time stamps

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

Open the .bashrc file

with a text editor such as Nano

: sudo nano .bashrc

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

: export HISTTIMEFORMAT=”%c ”

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

: %

  • d: Day
  • %

  • m: Month
  • %

  • y: Year
  • %H: Time%M: Minutes%S: Seconds%F: Full Date (Y-M-D format)%T: Time (H:M:S format)

  • %C: Full Date and Timestamp (DAY-D-M-Y H:
  • M:

  • S format)

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

: history

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 file .bash_history.

Editing the HISTSIZE and

HISTFILESIZE values changes the way the Bash shell displays and saves command history.

For example, changing the HISTSIZE value to 10 causes the list of history commands to display a maximum of 10 most recent entries

.

Save the changes to the .bashrc file, restart the terminal, and run the command history confirms the new output format: history Repeat a command Running the history command

Allows you to reuse any of the commands in the list. For example, to run the first command (sudo apt update) again, use:

!1

Add a hyphen () before the command number starts counting from the end of the list. For example, to reuse the last command (history 5), use

: !-10

Use double exclamation points to repeat the last command:

!!

Find

a command by string

Adding a string after the exclamation point runs the most recent command that begins with that string. For example, to reuse the most recent

command that begins with sudo, use: !sudo

Using this method can cause problems if the shell executes an unexpected command, especially when searching for a command that begins with sudo. As a precaution, adding the :p argument displays the command without executing it, allowing you to review the command and decide whether to run it.

!sudo:p

To find a command that contains a string but does not begin with it, add a question mark next to the exclamation point. For example, to reuse the last command containing echo:

!? echo

In the example above, the shell reuses the last command containing the string echo even though the command begins with sudo

.

List matching commands

The combination of 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 Change the executed command Use the following syntax to change the last

command executed

: ^[old string]^[new string]^ For example, the ufw command to enable port 20 shows that the port is already enabled: sudo ufw allow 20/tcp Use the

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

^20^22^ Prevent recording of commands in history To prevent

recording

commands in

the history

list, Temporarily disable recording 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 example, delete command number

87 with: history -d 87 Use the -c option

to clear the entire history list: history -c

Update the history file

The Bash shell saves 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 allows you to append this session’s command history entries to the .bash_history:history -a file Another method is to use the -w option to save the entire history list to the

file .bash_history:history -w

Conclusion

after reading this tutorial you should be able

to use the

history command on Linux to view, edit and delete the command history list and reuse the commands from it.

If you’re interested in learning more about Linux commands, take a look at our Linux command cheat sheet.

Contact US