The command history is stored in ~/.bash_history. The history typically contains the last 500 commands typed at the shell prompt.
You can view the history by using the history command.
history | less
Then you can search for a command.
e.g. what was the name of that directory I created yesterday? Oh, yeah, I can search history for the mkdir command by using the slash.
You could also use grep, but with less, you can see the surrounding commands, as you often want to see the sequence of what was done.
- !! - Executes the last command
- !902 - Executes command number 902 (from the history list)
- !service - Executes the command whose string matches the most recent history entry
- Up / Down - The most common shortcut is to simply use the up and down arrows to scroll through the bash history.
If you recently restarted the network service, and then wanted to restart it again, you might do something like this;
service network restart
You see that the network is unreachable.
Then you remember that you misconfigured something, such as gateway so you modify it:
Now, rather than typing the entire command again "service network restart", you can simply type:
A similar shortcut can be used when you forget to type sudo.
$ mkdir /root/test
mkdir: cannot create directory `/root/test': Permission denied
$ sudo !!
sudo mkdir /root/test
Or, to change the previous command, while keeping the parameter, use "!$"
e.g., change cat to vi, but keep the rest of the line
$ cat /etc/resolv.conf
$ vi !$
see also http://systemnotesorg.blogspot.com/2007/03/unix-linux-command-tips.html