Use “history” command to get the list of the history of command execution in the terminal.
Check History
Just use the “history” command without any option to get the full history of command execution.
$ history
2045 whatis apropos
2046 apropos ch
2047 apropos chm
2048 apropos his
2049 reset
BashExecute Specific Command from History
From the list, we get the serial number of the command execution. Say we want to execute the 2047th command then we have to write the exclaimation sign(!) then the number.
It will execute that specific number-
$ !2047
apropos chm
ab (1) - Apache HTTP server benchmarking tool
chmem (8) - configure memory
chmod (1) - change file mode bits
chmod (2) - change permissions of a file
fchmod (2) - change permissions of a file
fchmodat (2) - change permissions of a file
redis-benchmark (1) - Benechmark a Redis instance
BashExecute Specific Command from Last
To execute any specific command from the last, use exclamation and then hyphen (!-) and then the position of the command from the end.
Say we want to execute the 3rd command from the last(tail of the command execution history), then we have to execute the following command-
$ !-3
# Execute the 3rd command from last
ls -la
total 16
drwxrwxr-x 4 bigboxcode bigboxcode 4096 Jun 18 19:54 .
drwxr-xr-x 6 bigboxcode bigboxcode 4096 Jun 29 17:37 ..
drwxrwxr-x 12 bigboxcode bigboxcode 4096 Jun 16 23:17 laravel11
drwxrwxr-x 4 bigboxcode bigboxcode 4096 Jun 18 20:02 openswoole
BashExecute the Last Command
Use the exclamation mark twice(!!) to execute the last command-
$ !!
ls -la
total 16
drwxrwxr-x 4 bigboxcode bigboxcode 4096 Jun 18 19:54 .
drwxr-xr-x 6 bigboxcode bigboxcode 4096 Jun 29 17:37 ..
drwxrwxr-x 12 bigboxcode bigboxcode 4096 Jun 16 23:17 laravel11
drwxrwxr-x 4 bigboxcode bigboxcode 4096 Jun 18 20:02 openswoole
BashSearch Command in History
We can also search commands in the history. Type CTRL + R to start the search.
Then we just type what we are searching for-
$ CTRL + R
(reverse-i-search)`chmod': chmod 775 www/test/
BashClear Command History
To clear the command history completely, use the “-c” flag with history-
$ history -c
# This will clear the full commad history
BashOmit Command from History
To omit some command execution from history add a space before the command. Add a space before the command will ignore it from history-
$ pwd
# Note: there is a space before the command "pwd"
Bash