You want to be able to repeat commands on the command line without having to type them again.
Use the up and down keys to select previous commands from the command history, and the history command with grep to find older commands.
Discussion
You can access the previous command you ran by pressing the up key. Pressing it again will take you to the command before that, and so on. If you overshoot the command you wanted, the down arrow key will take you back in the other direction.
If you want to cancel without running the selected command, use Ctrl-C.
Over time, your command history will grow too large to find a command that you used ages ago. To find a command from way back, you can use the history command.
$ history 1 sudo nano /etc/init.d/my_server 2 sudo chmod +x /etc/init.d/my_server 3 /etc/init.d/my_server start 4 cp /media/4954-5EF7/sales_log/server.py myserver.py 5 /etc/init.d/my_server start 6 sudo apt-get update 7 sudo apt-get install bottle 8 sudo apt-get install python-bottle |
This lists all your command history and is likely to have far too many entries for you to find the one you want. To remedy this, you can pipe the history command into the grep command, which will just display results matching a search string. So, for example, to find all the apt-get (Recipe 3.16) commands that you’ve issued, you can use the line:
$ history | grep apt-get 6 sudo apt-get update 7 sudo apt-get install bottle 8 sudo apt-get install python-bottle 55 history | grep apt-get |
Each history item has a number next to it, so if you find the line you were looking for, you can run it using ! followed by the history number, as shown here:
$ !6 sudo apt-get update Hit http://mirrordirector.raspbian.org wheezy InRelease Hit http://mirrordirector.raspbian.org wheezy/main armhf Packages Hit http://mirrordirector.raspbian.org wheezy/contrib armhf Packages ….. |
See Also
To find files rather than commands, see Recipe 3.22.