How to View Your Command History in Linux
Remember that one command that solved your problem? Was it cat, less, more, wc or something else? When we’re at the terminal, we can issue dozens of commands to solve a problem and in the background our Linux OS is recording these commands to a history file.
In this how-to we’ll look at various ways of searching and re-using our command history. Whilst you become accustomed to these commands it’s important to double check you don’t unintentionally reissue a command that could cause problems. Take your time using these new techniques and double check the details before pressing enter!
All the commands in this how-to will work on most Linux machines. We’ve used a Ubuntu 20.04 install but you could run this how-to on a Raspberry Pi. All of the how-to is performed via the Terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.
Scrolling Through Your Previous Commands in Linux
The simplest way to look through your recent commands is to use the up and down arrow keys on your keyboard to scroll through the previous commands. If you want to reissue a found command simply press the enter key.
Viewing Your Command History in Linux
The history command, in its most basic use case, lists and annotates the last 1000 commands issued in the terminal emulator. Each command has a number associated with it.
1. Run the history command to see a list of the last 1000 commands. You’ll see that all the listed historical commands are given a unique reference number.
history
2. Reissue the history command but constrain the amount of results to a specific number. This is useful if you know roughly when a command you are looking for was issued. You should see only the last 20 results listed.
history 20
Reissue a Previous Command
Now we can use history to view our previous commands, we can choose and reissue a command using the number assigned to the history results.
1. Run history 20 to create a list of commands, choose a command to reissue making sure that the chosen command is safe to run. Choosing a simple command like cd Music (1660) is a good safe example. Note there is no space between the exclamation mark and the command number.
history 20
!1660
Read more: https://www.tomshardware.com/how-to/view-command-history-linux