4.3. Basics

This chapter starts by introducing some general information about working with the command line. From there, it explains how to: locate yourself in the file system, move and manipulate files, browse to other directories, and view their contents. Finally, it moves on to more complicated issues including re-direction and permissions.

4.3.1. Shell Prompt Terms

Below are a few terms to know before beginning the discussion of the shell prompt.

command line

The command line is where options to a command are placed. The following is an example of a command line:

command -options <filename>

shell prompt

The shell prompt is the marker on the screen that shows where the command line should be placed. The following is an example of a shell prompt:

[username@localhost.localdomain username]$

shell

The shell is the program that interprets commands so that the operating system can understand them.

terminal window

The terminal window is the window that contains the shell prompt, command line, and output from the shell.

4.3.2. Opening and using a Shell Prompt

Recall from Section 1.5 Opening a Shell Prompt that a terminal window is opened from the Applications (the main menu on the panel), or by right-clicking on the desktop and selecting Open Terminal.

Entering or running a command refers to typing a given command and pressing [Enter]. To close a terminal window, either click on the [X] in the upper right corner of the screen or enter the exit command at the shell prompt.

The shell prompt within a terminal window looks something like this:

[username@localhost.localdomain username]$

There are any number of symbols that can be used to indicate the end of the shell prompt, and you can customize what your prompt looks like. However, there are two symbols that you will see more often than any others, "$" and "#". The first symbol, "$", is the last character in the prompt when you are logged in as a normal user. The shell prompt for a normal user looks something like this:

[username@localhost.localdomain username]$

The second symbol, "#", is the last character in the prompt when you are logged in as root. This is true whether you logged in as root from the initial screen or if you executed the su - command to become root. The shell prompt for root looks something like this:

[root@localhost.localdomain root]#

This slight difference can help remind you what privileges you currently have.

4.3.3. Structure of shell prompt commands

In general, a command run from the shell prompt will have the following format:

command -options <filename>.

Both -options and <filename> are optional: the command may not require either one, or it may require multiple options and files. When specifying multiple options, list them as a group. For example, to see a long listing of information (-l) about all files (-a) in a directory, you would run the command:

ls -la

There are many variations in required information types for individual commands. If you aren't sure about a command and how it is used, you can do one of three things:

4.3.4. Useful tips for the bash shell

Below are a few useful features of the bash shell that reduce the amount of typing required at a shell prompt. The first of these is tab completion, the second is command history, and the third is useful keystrokes.

4.3.4.1. Tab Completion

Tab completion is one of the most useful shortcuts available at the command line. Red Hat Enterprise Linux has the ability to "guess" what command, directory, or filename you are entering at the shell prompt. Press the [Tab] key, and the shell offers possible completions for partial words. The more letters typed before pressing [Tab], the closer the shell comes to the intended command.

If there are multiple solutions to the partial text on the command line, the shell presents them as a list. If the list is very long, the shell will first ask if you would like to display all matches. Navigate long lists of potential matches by pressing the [Space] bar to go down one page, the [B] key to go back one page, the directional (or "arrow") keys to move one line at a time, and [Q] to quit.

The shell assumes that the first word entered at the prompt is a command. The possible completions it offers are the names of commands or applications. This can be helpful if you are not sure of the exact spelling of a command or if you are searching for a certain command. It can also serve to help a new user become familiar with the available commands.

For example:

  1. Type the letter g at a prompt and press [Tab] twice.

  2. The shell asks if you want to see all 379 possibilities. This means that there are 379 commands that start with the letter "g". Searching through this list would take too much time.

  3. Press [N] for no.

  4. Entering more of the command name will produce a shorter list of possible matches. For this example, type gnome and press [Tab] twice. A list of every command that starts with "gnome" appears. This is a much shorter list, and can be scrolled through using the same keys as man pages. Scroll to the end of the list to return to the shell prompt. The letters "gnome" are still entered.

  5. To finish entering a command with tab completion, enter just a few more characters, "-ca", and press [Tab] twice. The shell returns a match of gnome-calculator, and if you then press [Enter], the GNOME Calculator application starts.

Tab completion also works on filenames and directories. The shell prompt assumes that the second word on the command line is a filename or directory. Typing a partial word and pressing [Tab] twice will generate possible completions according to the files and sub-directories in your current working directory. The command line "knows" your directory structure. You can use tab completion to enter a long string of sub-directories by typing the first few letters of each directory and pressing [Tab] instead of navigating to one subdirectory at a time.

For example, reaching the sub-directory

example/sub-dir1/sub-dir2/sub-dir3/

would take a great deal of repetitive typing. However, with tab completion, a user would only have to enter a few keystrokes:

$ cd ex[Tab]su[Tab]su[Tab]su[Tab]

4.3.4.2. Command History

It is unnecessary to type the same command over and over. The bash shell remembers your past commands. These commands are stored in the .bash_history file in each user's home directory. To use the history, press the up arrow to scroll backward through the commands you have already entered. The [Ctrl]-[R] shortcut searches through your previous commands. Press [Ctrl]-[R] and type the beginning of the command you previously issued. The command history stops at the most recent version of that command.

Commands that you only typed partially and did not follow with [Enter] are not saved into your command history file. To clear your command history, type history -c.

By default, Red Hat Enterprise Linux stores 1000 commands. Each terminal window or shell prompt stores a separate set of commands. If you gain root privileges by using the command su -, the history file (and thus the commands) you access are root's, not the user's.

4.3.4.3. [Ctrl]-[Z] and running processes in the background

Applications and processes can be started from the command line. When an application starts from the command line, that particular shell is taken up with standard output for that application until the application closes. The screen fills with gibberish or messages that can be ignored. To continue to use the current shell while running an application from the same shell, add the ampersand, "&", to the end of the command line. For example, oowriter & starts OpenOffice.org Writer and allows you to continue entering commands on the command line. This is known as running a process in the background.

If you have started an application or process and forgotten to add the &, first press [Ctrl]-[Z] — this suspends the application. To allow it to continue running without displaying standard output, type bg and press [Enter]. This is referred to as running the application in the background.

4.3.4.4. Wildcards

Wildcards are place holders used to allow users to search for or use multiple files with similar names. The subject of wildcards is part of the larger subject of regular expressions. Two of the most common wildcards are "*" and "?".

The asterisk, "*", represents any character or string of characters. The entry a*.txt could refer to ab.txt as well as aardvark.txt.

The question mark represents a single character. The entry a?.txt could refer to ab.txt and a1.txt, but not aardvark.txt.

What if you forget the name of the file you are looking for? Using wildcards or regular expressions, you can perform actions on a file or files without knowing the complete file name. Type out what you know, substituting a wildcard for the remainder.

TipTip
 

To read more about wildcards and regular expressions, take a look at the bash man page (man bash). You can save the file to a text file by typing man bash | col -b > bash.txt. Then, you can open and read the file with less or with an editor such as vi (vi bash.txt). If you want to print the file, be aware that it is quite long.

For example, to find a file called "sneaksomething.txt," enter:

ls sneak*.txt

The shell lists every file that matches that pattern:

sneakers.txt

Regular expressions are more complex than the straightforward asterisk or question mark.

When an asterisk, for example, just happens to be part of a file name, as might be the case if the file sneakers.txt was called sneak*.txt, that is when regular expressions can be useful.

Using the backslash (\), you can specify that you do not want to search out everything by using the asterisk, but you are instead looking for a file with an asterisk in the name.

If the file is called sneak*.txt, type:

sneak\*.txt

Here is a brief list of wildcards and regular expressions:

  • * — Matches all characters

  • ? — Matches one character

  • \* — Matches the * character

  • \? — Matches the ? character

  • \) — Matches the ) character

4.3.4.5. Useful Keystrokes

While working from the command line, there are a few useful keystrokes that can help you with your session.

The following keystrokes are useful shortcuts at a shell prompt.

KeystrokeAction
[Ctrl]-[K]While editing a command on the command line, this key combination deletes everything that has been typed in from the cursor's current position forward.
[Ctrl]-[D]Pressing this key combination once ends the current application or process. Pressing it twice exits the shell.
[Ctrl]-[R]At the command line, [Ctrl]-[R] searches through the command history to find the entry that starts with the letters you type.
[Ctrl]-[Z]Suspends the current application. Entering bg after [Ctrl]-[Z] causes a program to run in the background.
[Ctrl]-[C]"Kills" a program. This should be a last resort. Try stopping a program with other methods first.
[Ctrl]-[L]Clears the terminal window.

Table 4-1. Useful shell prompt keystrokes

4.3.4.6. Clearing and Resetting the Terminal

The terminal window begins to look crowded very quickly. You can always exit from the terminal window and open a new one, but there is a quicker and easier way to remove the contents displayed in the terminal.

To clear the terminal, enter the command clear at the shell prompt. The clear command clears the terminal, leaving only a new shell prompt at the top of the window.

You can also clear the screen using the keystroke [Ctrl]-[L].

Sometimes, you may accidentally open a program file or some other non-text file in a terminal window. When you close the file, you could find that the text you are typing does not match the output on the monitor.

In such cases, enter reset to return the terminal window to its default values.