Linux users execute several commands every day and these commands may change the system configuration. It is very important for the system administrators and the users to know which commands were executed. Linux keeps a meticulous record of all the commands that we execute and this record can be accessed using the history command.
In this tutorial, we will learn how to use the history command in Linux.
- A computer running Linux.
- A user account with sudo privileges.
- Some familiarity with Linux command-line interface.
The Linux history command displays the list of previously executed commands in the Bash shell and Korn shell. Every Linux command that you execute is considered an event and an event number is assigned to the event. You can recall any event and modify it if required.
The history is maintained for each terminal session. You can quickly reuse the commands and it significantly boosts your productivity. The history command-line utility is extremely helpful when you have to run lengthier and more complicated commands.
The Linux command history is stored in a history file, for example, .bash_history for Bash shell. This file is hidden and can be listed using the
ls -a command in your home directory. You can use the following environment variables to find more details about command history on your Linux computer:
- HISTFILE variable - points to the location of the history file.
- HISTSIZE variable - maximum number of cached commands. Once this number is reached, the oldest commands are automatically deleted.
- HISTFILESIZE variable - maximum number of commands the history file can store.
You can change the default value of these variables in the .bashrc file manually, or by using the export command.
The basic syntax of the history command-line utility is as follows:
How to Use History Command in Linux
The simplest form of using the history command in Linux is to just type history on your terminal:
The history command in Linux displays the list of all the commands that you executed since the beginning of the session. Once the number of commands reaches the threshold specified in the $HISTSIZE variable, the oldest commands are automatically deleted. When you close your terminal session, the history is saved in the history file.
The history list of past commands can be very long and it could become difficult to navigate to past commands using arrow keys. You may also need to search for the executed commands to check if any command modified system configuration. For this purpose, you can pair the history command with the grep, tail, or less commands.
To display all the mkdir matching commands that you ran on your computer, use the following history command:
history | grep mkdir
To display the last 5 commands in the history, use the following history command in Linux:
history | tail -n 5
To display the first 10 commands in the history, use the following history command:
history | head
You can also perform a recursive search in your command history using the Ctrl + R. You can type any command name in recursive to search:
(reverse-i-search)`mkdir': history | grep mkdir
In this example, we typed mkdir and the first result with the mkdir command showed. Press the Enter or Return key to execute this command.
Clear Terminal History
You may have to delete misspelled, potentially harmful, and wrong command from your Linux history. In Linux, you can delete selected or all commands from history. You must first find out the event number of the command in history to delete a selected command in Linux.
To delete a particular command from history, use the
-d option followed by the event number:
history -d 695
In this example, the command
695 sudo mkdir /home/steve is removed from the history and its event number is reassigned. You can also delete multiple events using the
Use the history command with the
-c option to clear the whole history:
Here the history is removed, and Linux starts recording history from the beginning. Deleting commands from history does not have any impact on the functioning of your Linux computer.
Linux enables you to disable history. When you disable the history, all the commands that you execute are not entered in the history file, and you cannot repeat commands. Set the value of the HISTSIZE environment variable to 0 (zero) to disable history:
export HISTSIZE=0 history
To enable history again on your Linux computer, set the value of HISTSIZset the value of HISTSIZE to higher than 0 (zero).
Repeat Used Commands
Command history is very useful to repeat used commands. You can repeat complex Linux commands without having to retype them, or with updates.
Use the Up arrow key once to view the previous command and press enter to execute it. When you press the up arrow key, the command is displayed on the screen. Keep pressing the up arrow key to find other executed commands.
To display the previous command that you executed, type Ctrl +P and press enter key. Keep pressing the Ctrl +P keys for finding other executed commands.
If you need to execute just the previous command, type:
In the same command, you can also use
sudo !! to execute the previously executed command as the sudo user.
Type !<event number> to execute the command with the specified event number:
Type !-5 to repeat the 5th past command.
Update History File
When you close a terminal session, the command history is automatically written to the history file, .bash_history. Similarly, when you start a new session, the list of previously executed commands is read from the file. The changes that you make to the command history are written to the file only when you close the terminal or log out of the current user.
If you want to open a new terminal window for accessing the complete history list, then you need to write the entire history to the history file. Use the following command in the first terminal window before opening the second terminal:
In order to write changes to the file, including modified or deleted commands, type:
You can also manually edit the file by opening the file in an editor of your choice, using the vi .bash_profile command.
By default, the history command displays the event number and the command name. Use the following command to add a timestamp to the command history:
export HISTTIMEFORMAT=’%F %T’ history | more
Limit History Lines
The number of commands recorded in the history file is controlled by the HISTSIZE and HISTFILESIZE environment variables. Once maximum capacity is reached the oldest commands will be replaced with new commands.
The HISTFILESIZE variable set the maximum number of commands to be stored in .bash_history file and HISTSIZE variable set the maximum number of commands to be stored in the cache memory.
The above command sets the history size to 300 which means only 300 commands will be stored in the file. You may use echo command to print the variable value as the output.
You can also manually edit the .bashrc file to modify these environment variables.
By default, all the commands are recorded in the history file, even the duplicate commands. You can use the following command to instruct your system to avoid recording duplicate commands.
If your system is set to ignore duplicates, then unset the environment variable to record all commands:
The HISTCONTROL=ignoredups command stops recording duplicates, but it does not remove the existing duplicates. Use the following environment variable to remove existing duplicate history entries:
This command deletes only the consecutive duplicates.
You can also set the HISTCONTROL environment variable to not record history for particular commands. For example, set the HISTCONTROL to ingorespace to instruct your system to not record the history of the commands that start with a space.
In this example, we executed three commands and the last command ls had a space before it. In this history, the ls command that had space before it did not show.
In this tutorial, we learned about the history command in Linux. The history command helps you list all the previously executed commands.
The history command is available on all major Linux distributions, such as Ubuntu, Debian, Fedora, and Red Hat. This command is very easy to use, and you can follow the examples in this tutorial to modify history on your Linux computer.