‘history’ command will display a list of last used commands in the shell on your system.
Sample syntax of :
$ history <number>
Like example of :
$ history 10
See your terminal after running the command, we have used
history -10. You can use any integer other than 10.
Here in the example, it displays list of last ten commands.
To clear your history use
$ history -c
“history -c” command will clear your entire command history. After clearing running a
history <n> will return an empty result.
Ctrl + r
‘Ctrl+r’ searches the commands from the history matching the specified characters that you type on your terminal.
Press Ctrl+r and then type any characters, the most recent match will be automatically shown in the terminal. The closest match is from the closest time interval from now. Note that, this won’t list entire results that matches the query, but will show only one command which matches first.
Like example of :
press 'ctrl+r'. ca
See your terminal after run the command, we have pressed ‘ctrl + r’ followed by two characters ca and it has found the last command matching characters ‘ca’. It is very helpful.
Linux Shell History Commands
Interacting with the terminal, you will be typing some commands very often and sometime typing with variations on those commands.
Sometimes it can be very irritating as well as a little bit time consuming to type same large commands over and over.
Bash shell provides some commands to repeat the history of the commands used. The shell makes it easy for us to repeat the commands we have used.
Shell History Search Command
Repeats last typed command.
Repeat other commands other than the last typed command.
Shows a list of older commands. Notice the first integer. This is the line number. If we use that number as
!n, the bash repeats that command.
Repeats the command at line number n. Then number n is the line number defined by the ‘history’ command.
Ctrl + r
Searches for a command in history, and executes it, when pressed enter.
Environment Variables which controls the history
Determines number of commands stored in the history.
Displays location of file that contain history.
To set number of commands stored in history file
To view the contents of an environment variable use echo.
$ echo $HISTSIZE
Keys In History Command:
|UP Arrow Key||It will scroll you backward in history.|
|Ctrl + p||It will scroll you backward in history.|
|DOWN Arrow Key||It will scroll you downwards in history.|
|Ctrl + n||It will scroll you downwards in history.|
|Ctrl + r||It will take you to the command matching the specified character.|
|Ctrl + g||It means escape from history searching mode.|