How to View the Command History in Linux Terminal?

One can simply press the up arrow key to scroll through typed commands in Ubuntu. However, what if you want to view the history of commands in the Linux terminal that you typed a week ago?

Just type: history
In default, the above command will list around 500 commands that you have recently typed.

To filter these commands, use history x
Here x is the number of the command you want to see. For example: history 5

And, to search for a specific keyword, use grep alongside the history command. For example: history | grep user

You can also recall a command by typing the (exclamation mark) ! followed by the entry number.
For example, type !13 and it will show the 13th command from the recent commands list.

If you want to run the very last command, use !-1 or !!

And, to run the commands numbered from the last, use !-2 or !-3

And finally, to clear the history of all commands typed recently, type:
rm ~/.bash_history && history -c

Leave a Reply

Your email address will not be published. Required fields are marked *