Question 3.23: You want to be able to repeat commands on the command line w......

You want to be able to repeat commands on the command line without having to type them again.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

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.

Related Answered Questions

Question: 3.34

Verified Answer:

Use the Linux df command: $ df -h Filesystem   ...
Question: 3.33

Verified Answer:

Use the Linux date command. The date and time form...
Question: 3.32

Verified Answer:

Edit the file ~/.bashrc using nano (Recipe 3.6), a...
Question: 3.31

Verified Answer:

Run the program or command in the background using...
Question: 3.30

Verified Answer:

Redirect the output to /dev/null using >. For e...
Question: 3.24

Verified Answer:

Use the Task Manager utility, which you’ll find on...
Question: 3.29

Verified Answer:

Use the pipe command, which is the bar symbol (|) ...
Question: 3.27

Verified Answer:

Use the > command to redirect output that would...
Question: 3.28

Verified Answer:

Use the cat command to concatenate a number of fil...
Question: 3.26

Verified Answer:

Use the lsusb command. This will list all the devi...