The history command in Linux allows us to view the history of the commands that we run in the terminal. You can set the maximum number of entries BASH should store as history in your .bashrc file.
For most Linux users, using the command line is a daily activity. Whether it is installing new programs, administering servers, or managing data, we use command lines for a lot of tasks.
But out of all the commands we use on a daily basis, how many are we able to recall when we need to use a command we used a while ago?
Thankfully, we have a useful utility in Linux is available to help us.
Whether you are a beginner who needs help remembering previously used commands, or an experienced user who doesn’t wish to risk mistyping a complex command, the history command will be an important tool for you.
Table of Contents
Why do we need the history command?
The history command in Linux is a utility supported by both Bash and Korn with the purpose of keeping track of previously used commands in the command line.
It works by treating every single command as a separate event. Then it associates each event with a number which is used to recall or modify a command when required.
This list of previously executed commands is stored in a history file at ~/ .bash_history on your machine.
The history command is used for any operation related to the commands which were used in the command line on your machine. It can list all the commands from the history file and repeat or modify any previously used commands.
Basic Usage of the history Command in Linux
Now that we understand how the history command in Linux works, it is time to learn how to use it. Here we explore some common useful applications of the history command.
1. Checking the command history
The most basic use of the history command in Linux is to display the previously used commands on your machine with their associated event number.
This can be done by typing
history in your command line. You should see an output screen similar to the screenshot given below.
As you can see here, the right column lists all the previously used commands in ascending chronological order from the moment the user entered their first command in the terminal on their machine.
The left column displays the ‘event number’ of each of those commands. The more recent a command is, the higher will its event number be in this list.
In this screenshot, the most recent command was the one with event number being 15.
2. Limiting the Output of the history Command
For most occasions, we don’t need the entire list of commands entered by us. more frequently we need a list of the most recent commands we used in the terminal.
This can be done by adding the number of recently used commands when you call the history command in Linux. Here is how you display the 5 most recently used commands in your terminal.
As you can see, the history command displayed only the last five commands entered in our terminal. If you work with the Linux command line for long periods of time, this is a command which you can see yourself using often.
3. Repeating past commands
Being able to see previously used commands on your terminal is only half the functionality of the history command in Linux.
This command also allows you to re-do the commands that you have already used in the past.
To repeat a command which you used earlier, you use an exclamation mark (!) followed by the event number of the command you want to repeat.
This is how we repeat the event number 20 using the history list.
This functionality is really important if you need to repeat a command over and over. It also essential when you need to repeat a complex command.
If you wish to repeat the last command, you don’t need to remember the event number of the command.
This can be done by simply entering the command
!! into your terminal. This will repeat the last executed command.
Sometimes you don’t remember the event number for a command, but you remember how many commands you have used before it.
We can repeat the n-th command we used earlier by using the following command.
Here num is the number of commands before now which we wish to repeat. Hence, to repeat the 6th recent command, we will replace num with 6 in the above command.
4. Deleting history
With the command history lists in Linux, you can see history, repeat it and even erase it.
We use the
-d tag with the history command to delete a command from the history list in Bash or Korn shells. Following is the syntax for deleting commands from your history list.
history -d [event number(s)]
As you can notice above, this can be used to delete a single command or a list of commands form your history list, provided that you know the event numbers for the concerned commands.
Another function of the delete command is to delete the last n commands from your history list. This can be done by entering the following command.
history -d -n
Here, n is the number of recent commands which you wish to delete from the history list. This means that to delete the last five commands, you replace n with 5 in the above command.
The history command in Linux is an important tool for a user to view or repeat commands which have been previously used on their terminal. It helps to make the task of a command-line user simpler.
We hope this tutorial was able to help you understand the history command in Linux. If you have any feedback, queries or corrections, feel free to leave them in the comments below.