Linux is a command-based operating system that relies primarily on commands to execute tasks. During a terminal session, you run various commands; noting them is not feasible as it can be time-intensive. That’s why the history command is handy to view previously run commands in the terminal.
It helps you recall and reuse earlier commands and troubleshoot unexpected system behavior. So this short blog will briefly explain how to use the history command in Linux, including its usage, options, and some examples.
How To Use the history Command in Linux
You can run the below command to check the history of the previously executed commands:
history
The above command, by default, shows a list of up to 1,000 commands. If you wish to view a specific number of commands, go for the below command:
history N
Where N is the required number of preceding commands. For example, to see the last 3 commands executed, we will enter:
history 3
Please combine history with the grep command to search for a particular earlier command. For instance, to search for occurrences of cd:
history | grep cd
If you want to reuse any previous commands, please check that command’s line number. For example, let’s reuse the cd ~/Documents available in the 9th row:
!9
If you want to clear the history, then please use the -c option:
history -c
Moreover, the ‘-d N’ option deletes a specific entry at N. For example, let’s delete the history from raw 200 to 275:
history -d 200-275
A Quick Wrap-up
The history command is a valuable tool in Linux’s range of commands. It allows you to recall and manage previously executed commands. This blog demonstrated how to use the history command with practical examples. Furthermore, we explained two primary options to manage the command history.
View the full article