The ‘history’ command available in Bash can be used to simply display your shell history, however there’s also a whole lot more that you can do with it, which we’ll demonstrate here.
Bash history allows us to quickly see what has been executed previously on a system, allowing you to hold users at least somewhat accountable for their actions (more on this later). It’s also useful if you’ve run something before and forgot the command, I can’t begin to tell you the number of times that I’ve done this!
In this article I will show some basi history command example.
History Command Example
First, connect to your server via SSH.
# ssh [email protected]
Let’s start with the simple command:
You should now see a list quickly go by with the last 500 commands used, like the example below. If you like, you can just use the up arrow and down arrow to browse for any particular command you may have used recently.
496 ls -la 497 ls 498 history 499 ls 500 cd domains 501 cd .. 502 ls 503 history 504 cd ls 505 ls 506 cd data 507 ls 508 cd .. 509 cd domains 510 ls 511 cd .. 512 history
If you wish to view the history one page at a time, you can use the command below. Now, you can simply use the spacebar to view one page at a time or use the down arrow to view one line at a time:
# history | less
To view just the last ten commands, you can use the following:
# history | tail
To view the last 25 commands, just use the following:
# history 25
Another tool you can use with history is Ctrl + R. This will output a search feature. Just begin typing a command and it will complete the command with the most recent match. If it is not the one you need, simply type a few more letters until you find the command you wanted. Once you find it, simply press the return key to run or press the right arrow key to edit it.
Another way to search history is with the following command (just be sure to replace “searchterm”):
# history | grep -i searchterm | less
As you can see, the history command can be very useful in finding the last commands that have been used on your server.