Becoming Comfortable at the Command Line


You're already familiar with a few file management commands from Chapters 6 and 7, such as ls, mv, cp, and rm. Feel free to try out a few of them now, just to get a feel for the way virtual consoles interact. For example, try getting a long directory listing of the /bin directory. Recall that you obtain long directory listings by appending the -l argument to the ls command, so the command to get a long listing of the /bin directory is

 [you@workstation20 ~]$ ls -l /bin 

The results of this command are shown in Figure 18.3.

Figure 18.3. The long directory listing of the /bin directory, generated by typing ls -l /bin.


Feel free to try out a few more commands now. For example, get a directory listing of your home directory or navigate the file system a little bit using the tools you gained in Chapter 6. Feel free to experiment.

Scrolling Content in Virtual Consoles

As you viewed the tail end of the directory listing shown in Figure 18.3, you saw some familiar things: executable files, symbolic links, and file permissions flags. You probably also noticed, however, that most of the directory listing had scrolled past the top of the console.

While using a virtual console, you'll often find yourself wishing you could see what commands or what outputs have scrolled past the top of the screen. As you might imagine, there is a way to do just that:

  • To scroll up (to see progressively older commands and output) at a virtual console, hold down your Shift key and repeatedly press the Page Up (PgUp) key.

  • To scroll down (to see progressively more recent commands and output) at a virtual console, hold down your Shift key and repeatedly press the Page Down (PgDn) key.

Note that if you have scrolled up by pressing Shift+PgUp repeatedly, you don't need to return to the command prompt by pressing Shift+PgDn repeatedly before you can enter a new command. Just begin typing a new command, and the display automatically returns to the most recent command prompt.

Using Command History

As you begin to have longer sessions at the command line, performing more complex tasks and in greater quantities, you will begin to find yourself wanting to repeat commands that you entered earlier or to recall a previously entered command that gave the results you wanted.

A feature called the command history allows you to do just that. To view the command history for a Terminal or virtual console session you're working in, just type the word history at the command prompt. The history command displays a chronological list of the commands you've entered and associates each one with a number. Sample output from the history command in a virtual console is shown in Figure 18.4.

Figure 18.4. The output from the history command lists entered commands in chronological order, from oldest to newest. Note that the history command itself appears in the list, too.


As you can see, the output of the history command gives you a concise list of all the commands you've recently run. Notice, too, that each command is prefaced by a number. After using the history command to see the list of recently run commands, you can repeat any of them simply by entering an exclamation point followed by the number of the command in question. For example, Figure 18.4 shows the command

 ls l ~/sharedjackfiles 

Assuming you're the user of the virtual console shown in Figure 18.4, to repeat this command, you need only type

 [you@workstation20 ~]$ !84 

to repeat the command once again just as if you had typed it at the command prompt.

Be Alert When Using the Command History!

Sometimes using a command from the command history can have unintended consequences if the circumstances surrounding the command are different. In particular, reusing potentially destructive file management commands such as rm, rmdir, or mv can have unwanted effects if your current working directory has been changed since the first time you used them.

Whenever you reuse a command from your command history, be sure to check your current working directory first and think carefully through the effects of reusing the command!


In addition to the history command, there is another way to access your command history that is often even more convenient. At any command prompt, simply press your up-arrow key to display the last command you entered, just as if you had typed it at the command prompt; you can then press Enter to reuse the command. (You may need to try pressing your own up-arrow key now to understand the effect.)

Pressing the up-arrow key repeatedly "steps back" through the commands in your history, showing earlier and earlier commands as if you had just entered them. Pressing Enter at any time executes the command as if you had typed it yourself.

Similarly, pressing the down-arrow key "steps forward" through the list of commands in your history if you have already pressed the up-arrow key a number of times.

Repeated Commands Become Part of History, Too

When you repeat a command from your command history, whether by entering an exclamation mark or by using your up-arrow key, the command that you repeated appears again as the newest command in your command history, just as if you had typed the command by hand.


Using Command-Line Editing Features

As you progress through the following few chapters, you'll also find yourself typing progressively longer commands. Because commands at the command line invariably involve punctuation keys, and because you're not yet terribly experienced with the command line, sometimes you might want to make changes to what you've already typed without having to retype the whole line.

Fortunately, the shell understands a number of basic editing keystrokes to help you edit your commands as you type them. These keystrokes are shown in Table 18.1.

Table 18.1. Command-Line Editing Keystrokes

Key

Action

Left arrow

Moves the cursor closer to the command prompt without erasing any of the characters you've typed

Right arrow

Moves the cursor closer to the end of the command without erasing any of the characters you've typed

Backspace

Deletes the character immediately to the left of the cursor

Delete

Deletes the character under the cursor


As you read through Table 18.1, note that any text you type when the cursor is not at the end of the command will be inserted wherever the cursor is. If you are familiar with nearly any word processor or text editor, the keys shown in Table 18.1 are probably familiar to you, and you'll use them often to edit complex commands as you enter them.

Note also that commands from your history can be edited this way, too. This capability provides a convenient way to use a command you've previously entered as a starting point for a new, slightly different command. To edit your command history, follow these steps:

1.

Step back through your command history by pressing the up-arrow key until you find the command that you want to reuse.

2.

When the command you want to reuse is visible on the command line, edit it as necessary using the editing keys.

3.

After you edit the command as desired, press Enter to execute it.

Keep in mind that the new, edited command will then show up as the most recent command in your command history; you can then re-execute or reuse the new command, too, for later commands.

These Techniques Work in the Terminal, Too!

Even though the figures in this chapter show commands and keystrokes being used in virtual consoles, keep in mind that techniques like the command history and command editing work identically when you are using the command line in the Terminal application on the Linux desktop. These features of the shell apply in either environment!




    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net