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.
- A system running
- An account with sudo privileges
. Access to the terminal
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:
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:
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
- %C: Full Date and Timestamp (DAY-D-M-Y H:
- S format)
%H: Time%M: Minutes%S: Seconds%F: Full Date (Y-M-D format)%T: Time (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
the size of the history buffer The .
bashrc file contains two entries that control the size of the history buffer
- 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:
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
Use double exclamation points to repeat the last command:
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.
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:
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
: ^[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
list, Temporarily disable recording using
: set +o history
To re-enable recording, use:
set -o history
history Use the -d option with the
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
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.