Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux Timesaving Techniques For Dummies.pdf
Скачиваний:
59
Добавлен:
15.03.2015
Размер:
15.98 Mб
Скачать

9 Making History

(Work for You)

Technique

Save Time By

Using history to recall previous commands

Including the history command number in your bash prompt

Filtering your history file to prevent accidents

Reusing complex command lines

Like any typical Linux user, you likely have a small core of commands, directories, and files that you work with. The bash shell keeps track of every command that you type in the history list — including those

commands that you use most frequently. You can take a peek at your history list in a file named ~/.bash_history, where, by default, bash stores the most recent 1,000 commands.

If you know how to move in and out of the history list with ease, history can save you a lot of keystrokes. With Linux, you can use the history list to recall previous commands, modify them if you need to, and execute them again without all that typing (and all those typing mistakes).

In this technique, we show you how to use the history file to save time at the command line. Less typing = fewer mistakes. Fewer mistakes = more commands that work the first time. More commands that work the first time = more time left for other things that you’d rather be doing.

Navigating the History List

To see the history list, type history and press Enter. With the history list ready and waiting for you, you can move through it in all sorts of ways. This section explains the different ways to get to the command you need — quickly. Table 9-1 gives you an overview of your options and when it’s best to use them.

Scrolling

You can scroll through the list by using the upand down-arrow keys:

Up arrow: Recalls commands starting with the most recent and moving towards the oldest. For example, press the up arrow once to see the previous command and press it again to see the command before that.

 

Navigating the History List

51

TABLE 9-1: NAVIGATING HISTORY QUICKLY

 

 

 

 

Navigation Method

When It’s Useful

 

 

 

Scrolling

Scroll through your history when you know that the command you’re looking for is close by. If you have

 

to scroll through more than five or six commands to find the one you want, use a different method.

Recalling by

If you include the history command number in your bash prompt (see Technique 4), you can recall

command

a specific command by number. That works great if you can see 20 or more commands on your screen

number

at once.

 

Searching

If the command you’re looking for is not close by (you’ve executed a number of commands since the

 

one you want to recall), press Ctrl-R to search for commands that contain a specific pattern. We show

 

you how in “Searching through history.”

 

 

 

 

Down arrow: Moves in the opposite direction — it starts at the current command and moves towards the most recent. (You can’t use the down-arrow key until you’ve used the up-arrow key. The down arrow won’t anticipate your next move and make up a command for you . . . we wish it did.)

If you want to use command numbers to refer to your history list, we recommend including the command number in your bash prompt; just include \! in $PS1. See Technique 4 for more information.

After you find the command that you want, change it if you need to and then press Enter.

Summoning a command by number

Each command is assigned a number when it’s placed in the history list. (The first command is command 1, and the numbers increase from there.) When you type history and press Enter to see the history list, you also see that a number precedes each command:

$ history

53ssh louvre

54ssh versailles

55pwd

56ls -l

57rm *.tmp

58mail franklin

59history

You can use the command number to recall a specific command. Just type an exclamation point ( ! ) and follow it with the number of the command that you want to recall. For example, to recall the ssh command in freddie’s history, you would enter the following:

$ !54

ssh versailles [freddie@versailles]

Searching through history

You can also ask bash to search through the history list on your behalf. Press Ctrl-R to start an incremental search. As you type each character, bash recalls the most recent command that includes the characters you’ve entered. For example, given the command history for freddie, an incremental search for ssh would go like this:

1. Press Ctrl-R.

The prompt changes from [freddie@bastille] to

(reverse-i-search)’’:

2. Type s (the first character in ssh).

bash finds the most recent command that includes an s (which is history), and the prompt changes to

(reverse-I-search)’s’: history

3. Type s again (the second letter in ssh).

bash finds the most recent command that includes ss (which is ssh -A versailles), and the prompt changes to

(reverse-I-search)’ss’: ssh -A versailles

Соседние файлы в предмете Операционные системы