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.
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
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
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 "
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
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.
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.
Saving the changes to the .bashrc file, relaunching the terminal, and running the history
command confirms the new output format:
history
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
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
Use double exclamation points to repeat 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
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
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
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
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
Use the syntax above to change the port number from 20 to 22:
^20^22^
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
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.