Monday, December 1, 2008

Finding Commands Quickly

14.5. Finding Commands Quickly

I was looking at a man page yesterday, but I can't remember the name of the command I was reading about, and I didn't write it down. How do I get the man page back?

 

The command you used will most likely be stored in a file called .bash_history. By default, this file records the last 500 commands you typed at the shell prompt. You can glimpse the history of your commands by typing history at the shell prompt, but the results will speed by too quickly for your to read ever line.

 

Another way to view .bash_history is with a utility such as less. Type less .bash_history at the shell prompt and the results will display one page at a time. To move forward a screen, press the [Space] bar; to move back a screen, press the [b] key, and to quit, press [q]. Paging through .bash_history to find a command can be tedious. Alternatively, you can search through the file for keywords using grep, a powerful search utility. Say you were reading the man page the day before, but cannot recall its name. To search for the command, type:

 

history | grep man

 

You will see a list of all the commands you typed which have the word man in them. There are plenty of ways to your command history. For other tips and tricks, see Section 14.6.

 

14.6. Tips on Using Command History

What are some other ways I can use command history?

If you type history, you will see a numbered list scroll by very quickly, showing you the previous 500 commands you have used. You probably do not need to see all of the last 500 commands, so the command history 20 might be useful. This way, only the previous 20 commands you typed will display (you can use any quantity as an argument of the history command).

 

14.6.1. Other Shortcuts

Here are other command history shortcuts which may be useful to you:

 

Ø      "Bang, bang": Typing !! (called "bang bang") executes the last command in the history.

Ø      "Bang number": Typing !number (as in !302) will execute the command which is numbered 302 in the history file.

Ø      "Bang string": Typing !string (as in !rpm) will execute a command with the most recent matching string from the history file.

Ø      [Up arrow] and[ down arrow]: At the shell or GUI terminal prompt, you can simply press the up arrow to move back through previous commands in your history list (the down arrow will move you forward through the commands) until you find the command you want. Press [Enter] to execute the command, just as if you had typed it on the command line.

 

14.7. Keep ls Output from Scrolling

Whenever I type ls I can barely see the output of the directory because it scrolls by too quickly. How can I actually read the output?

To prevent the output of ls from scrolling by too quickly, pipe the output to a utility such as less or more. You will then be able to see the output one screen, or "page" at at time.

 

To read the contents of /etc with less, type the following command at the shell prompt:

ls -al /etc | less

 

To move forward a screen, press [Space] bar; to move back a screen, press the [b] key; to quit, press [q].

 

You can achieve the same results with more, another paging utility.

 

14.7.1. Printing ls Output

You can also print directory listings by piping the output to a printer in the same way that you piped

the output to your screen. If you have configured a printer, type the following to pipe the output of a

command to the printer:

 

ls -al /etc | lpr

 

14.8. Forgotten Password

Help! I forgot my root password. How do I log in now?

You can log in using single-user mode and create a new root password. To enter single-user mode, reboot your computer. If you use the default bootloader, GRUB, you can enter single user mode by performing the following:

 

1. At the bootloader menu, type [e] to enter into editing mode.

2. You will be presented with a boot entry listing. Look for the line that looks similar to the following:

        kernel /vmlinuz-2.4.18-0.4 ro root=/dev/hda2

        Press the arrow key until this line is highlighted and press [e].

3. You can now add the word single once space after the end of the text to tell GRUB to boot into single-user Linux mode. Press [Enter] to make the editing change take effect.

4. You will be brought back to the edit mode screen. From here, press [b] and GRUB will boot single-user Linux mode. After it finishes loading, you will be presented with a shell prompt similar to the following:        sh-2.05#

5. You can now change the root password by typing:         bash# passwd root

You will be asked to re-type the password for verification. Once you are finished, the password will be changed. You can then reboot by typing reboot at the prompt; then you can log in to root as you normally would.

 

14.9. Password Maintenance

I forgot or want to change my user account password.

Open a shell prompt and type the following:

passwd username

 

Replace username with your normal user name.

The passwd will then ask for the new password, which you will need to enter twice. You can now use the new password to log in to your user account.

 

14.10. Changing Login from Console to X at Startup

How do I change my login from the console to the graphical screen?

Instead of logging in to your system at the console and typing the startx command to start the X Window System, you can configure your system so that you can log in directly to X. You must edit one file, /etc/inittab, by changing just one number in the runlevel section. When you are finished, reboot the computer. The next time you log in, you will have a graphical login prompt.

Open a shell prompt. If you're in your user account, su to root by typing

su

Password: yourrootpassword

Now, type pico /etc/inittab to edit the file with Pico. The file /etc/inittab will open.Within the first screen, you will see a section of the file which looks like this:

 

# Default runlevel. The runlevels used by RHS are:

# 0 - halt (Do NOT set initdefault to this)

# 1 - Single user mode

# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)

# 3 - Full multiuser mode

# 4 - unused

# 5 - X11

# 6 - reboot (Do NOT set initdefault to this)

#

id:3:initdefault:

 

To change from a console to a graphical login, you should change the number in the line id:3:initdefault: from a 3 to a 5.

 

Warning

Change only the number of the default runlevel from 3 to 5.

Your changed line should look like the following:

id:5:initdefault:

When you are satisfied with your change, save and exit the file using the [Ctrl]-[x] keys. You will see a message telling you that the file has been modified, and asking you to confirm your change. Type [Y] for yes. Now, your next login after reboot will be from the graphical screen.

No comments: