The “history” command in Linux is used to get previously used commands. As the name suggests this command gave the history of the system.
Let’s have look at the “history” command:
The Syntax for the “history” command
$ history [option]
This command gave a list of previously used commands with an event number associated with each command
$ history
1 sudo apt install build-essential dkms linux-headers-$(uname -r)
2 sudo dpkg --configure -a
4 sudo apt install build-essential dkms linux-headers-$(uname -r)
5 pwd
6 cd
7 /
8 pwd
9 cd /
10 pwd
.
.
.
To get the last n number of command use desired number after “history“. In the below example last 5 commands are retrieved
$ history 5
394 wc -c linux.txt
395 history
396 man history
397 history
398 history 5
We can execute a command from history using the event number and “!”
$ !396
man history
We can execute the most recent command by using “!!”
$ !!
man history
The whole history can be removed using the “-c” option with the history. And the cleared history can be verified using the “history” command once again
$ history -c
$ history
1 history
Here we have discussed the “history” command.
Have a look at other Basic Linux commands on the website.