OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Searching History

At any time, we can view the contents of the history list by:



[me@linuxbox ~]$ history | less

[me@linuxbox ~]$ history | less


By default, bash stores the last 500 commands we have entered, though most modern dis- tributions set this value to 1000. We will see how to adjust this value in a later chapter. Let's say we want to find the commands we used to list /usr/bin. One way we could do this:



[me@linuxbox ~]$ history | grep /usr/bin

[me@linuxbox ~]$ history | grep /usr/bin


And let's say that among our results we got a line containing an interesting command like this:

88 ls -l /usr/bin > ls-output.txt

The number “88” is the line number of the command in the history list. We could use this immediately using another type of expansion called history expansion. To use our discov- ered line we could do this:



[me@linuxbox ~]$ !88

[me@linuxbox ~]$ !88


bash will expand “!88” into the contents of the eighty-eighth line in the history list. There are other forms of history expansion that we will cover a little later.

bash also provides the ability to search the history list incrementally. This means that we can tell bash to search the history list as we enter characters, with each additional char- acter further refining our search. To start incremental search press Ctrl-r followed by the text you are looking for. When you find it, you can either press Enter to execute the command or press Ctrl-j to copy the line from the history list to the current command line. To find the next occurrence of the text (moving “up” the history list), press Ctrl-r again. To quit searching, press either Ctrl-g or Ctrl-c. Here we see it in action:


[me@linuxbox ~]$

[me@linuxbox ~]$


First press Ctrl-r:


(reverse-i-search)`':

(reverse-i-search)`':


The prompt changes to indicate that we are performing a reverse incremental search. It is “reverse” because we are searching from “now” to some time in the past. Next, we start typing our search text. In this example “/usr/bin”:



(reverse-i-search)`/usr/bin': ls -l /usr/bin > ls-output.txt

(reverse-i-search)`/usr/bin': ls -l /usr/bin > ls-output.txt


Immediately, the search returns our result. With our result, we can execute the command by pressing Enter, or we can copy the command to our current command line for fur- ther editing by pressing Ctrl-j. Let's copy it. Press Ctrl-j:


[me@linuxbox ~]$ ls -l /usr/bin > ls-output.txt

[me@linuxbox ~]$ ls -l /usr/bin > ls-output.txt


Our shell prompt returns and our command line is loaded and ready for action! The table below lists some of the keystrokes used to manipulate the history list:

Table 8-5: History Commands


Key Action

Key Action

image

image

image

Ctrl-p Move to the previous history entry. Same action as the up arrow. Ctrl-n Move to the next history entry. Same action as the down arrow. Alt-< Move to the beginning (top) of the history list.

Alt-> Move to the end (bottom) of the history list, i.e., the current command line.


image

Ctrl-r Reverse incremental search. Searches incrementally from the current command line up the history list.


image

Alt-p Reverse search, non-incremental. With this key, type in the search string and press enter before the search is performed.


image


image

Alt-n Forward search, non-incremental.


image

Ctrl-o Execute the current item in the history list and advance to the next one. This is handy if you are trying to re-execute a sequence of commands in the history list.


image


Top OS Cloud Computing at OnWorks: