Command line history

Linux shells maintain a history of the commands issued by a user. You can display the list of commands that you’ve entered before with the history command. This is a neat shell feature because it enables you to repeat some complex commands you’ve already entered. By default, the last 500 commands are shown.

linux history command

You can also execute a command by its number in the history. For example, in the picture above, you can see that the man -k shell command is at number 17. To execute that command again, we would type !17:

linux execute a command using the history command

The shell enables you to retrieve the previous entry from the command history by pressing the Up arrow on your keyboard. You can also press Ctrl+r to search your command history. For example, to search the history for commands that include dd, we would press Ctrl+r and then type dd:

linux search the history using ctrl r

To find the next command in history that includes dd, we can press Ctrl+r again.

Typing history -c clears the history. Use this option if you have typed commands that include passwords.
Geek University 2022