LINUX TUTORIAL

LINUX BASICS

What Is the Linux Shell?

To use Linux--or any Unix

-like system, for that matter--you need to know a few things about shells. A shell is a program that acts as an intermediary between you and the guts of the operating system. In a DOS environment, command.com acts as your shell. Linux shells have more interesting names (like bash, pdksh, and tcsh), but they do pretty much the same thing. In addition to translating your commands into something the kernel can understand and act upon, the shell adds some important functions that the base operating system doesn't supply

Using a Linux shell means working with a command line, which is much like working from a DOS prompt. Modern version of Linux come with graphical user interface (GUI) but some Linux tasks can only be done from the command line. The knowledge of how Linux works that you'll gain in this section will provide the foundation you need to use Linux successfully and efficiently. Here's a description of the basic features of all Linux shells, a preview of the functions they perform, and a rundown of what you'll learn in this section.

PromptsA prompt is a character or string of characters (such as $ or #) that the shell displays when it is ready to receive a new command. You'll learn about the different types of prompts and how to customize them to suit you and the way you work.

Command resolutionWhen you enter a command, the shell must determine which program to run in order to perform that command. You'll learn how shells do this and how to change the command resolution process to suit your needs.

Job controlLinux lets you multitask (run more than one command at a time). You'll learn how to start, list, and stop tasks; you'll also learn the difference between foreground and background task execution.

Command history and completionWhen you're entering lots of commands, sometimes you want to repeat the previous command or issue a similar one. You'll learn how to recall and modify previously entered commands, as well as find out about some keyboard shortcuts that can automatically complete your commands for you.

Wildcards and aliasesWildcards let you process a whole bunch of files at once, instead of having to repeat the same command for each file. You'll learn how to use two types of wildcards and how to create aliases for commonly used commands.

Piping and I/O redirectionSending the output of one program directly to another program or to a file can save you time and keystrokes. You'll learn how to pipe program output (connect programs together) and how to make a program get its input from a file instead of the keyboard.

This section will look at each of these functions in detail and teach you how to use them to your advantage. But first--a few shell preliminaries.

For more information on the bash shell, see the bash manual.

How is the Root User Different Than Others?

The Root User

In any Unix system, the root user is called a superuser because it has powers far beyond those of mortal users (and it's not even affected by kryptonite).

As root, you can access files and change the system in ways other users cannot. But you can also wipe out your entire hard drive in just ten keystrokes.

Unless you plan to install new software

or tweak your system, you should log in to Linux as a user other than root.

Ex Nihilo: Adding New Users

In the Linux universe, new users do not evolve--they are created by a benevolent superuser. To add a new user account, log in to the root account and enter a command like the one shown here. There's no limit to the number of new users you can add.

adduser hermie

After using the adduser command, you must assign a password to the new account before it can be used. Use this command to set the password:

passwd hermie

. . . and enter the initial password for hermie when prompted.

What is a Linux Virtual Console?

What Virtual Consoles Are Good For

Remember that bit about multiple log-ins? Even though you may not have more than one physical console (a monitor plus a keyboard) connected to your PC, you can use virtualconsoles to log in simultaneously to more than one account on your system

You can use virtual consoles to perform two activities in parallel. For example, I used one virtual console to write this section and another to test the commands as they were introduced. You can even use your mouse to cut and paste text from one virtual console to another. When you start your Linux system and get the log-in prompt, you're looking at console number 1. Go ahead and log in as root here; then press alt-F2. You should then see another log-in prompt. You can log in as userhermie on this console and then press alt-F3 to access a third console or press alt-F1 to return to the first console.

Virtual consoles come in particularly handy if you have a long-running task to perform, like installing a big software package from a CD-ROM--you can pop over to another console and log in again to stay productive while your CD-ROM churns away.

Note: You don't have to use a different user account for each console. Linux lets you log in to an account multiple times simultaneously.

By default, your Linux system already has a bunch of virtual consoles waiting in the wings when you start your system, and pressing alt-Fn at any time will bring the nth one up on your screen. You can also cycle through the consoles with alt-left arrow or alt-right arrow.

Multitasking under Linux isn't really much different from having multiple windows active on a Windows or Macintosh system. The major difference is that if you've started multiple consoles, you can see only one at a time on the screen, though the others are still working away behind the scenes.

Do I Have to Logoff and Shutdown My Linux PC?

Stop the System, I Want to Get Off!

If you're ready to cash in your console and call it a day, use the logout command. Entering

logout

at the command prompt

exits your current user account and returns you to the log-in prompt. (The exit command does the same thing as logout.) To log out from multiple consoles, use alt-Fnto switch between consoles and then log out from each one. But note that even if you log out from all of your active consoles, Linux is still running.

If you were to power off your machine at this point, a voice from your computer would drone, "You have chosen unwisely!" The floor would shake, and your PC would glow white hot while your hard disk melted into a pool of molten silicon. Just kidding . . . sometimes the floor doesn't shake, but powering off a running Linux system without using the shutdown command will most certainly cause Bad Things to happen to your hard disk. So if you really want to exit Linux, be sure that you're logged in as the root user and enter the command

shutdown -h now

You'll see some messages indicating that various subsystems are being shut down, and then the computer will be reset. When you see a message indicating that shutdown is complete, it's safe to turn off your PC.

Tip: Pressing ctrl-alt-delete will also safely shut down your Red Hat Linux system. Just remember to power off as soon as you see your PC's normal bootup screen. Note that some other Linux distributions will do a hard reset (which may cause filesystem damage) instead of gracefully shutting down, so don't use ctrl-alt-delete on non-Red Hat systems unless you know it's safe.

Which Linux Shell Should I Use?

Bash (and Other Shells)

Bash, the default Linux shell, is popular and offers lots of advanced features. It combines many of the niceties found in the Bourne shell (the original Unix shell) and other modern shells.

But there are alternatives. The Korn shell is popular among many Unix users because it offers a rich scripting (programming) facility. Korn is commercial software, but there is a public-domain (free) version for Linux called pdksh. Another shell that's popular with C language programmers is the C shell, which exists for Linux as tcsh.

Most versions of Linux provide both pdksh and tcsh, and you can try them out by entering their names at your shell prompt. If you decide to make a permanent switch to something other than the default bash shell, you must log in as root and edit the entry for your user ID in the /etc/passwd file.

Entries in the /etc/passwd file look like the following example. Just change "bash" at the end of the line to "pdksh" or "tcsh," and you're done. (If you're not familiar with Linux-based text editors, see "Text Editor s").

hermie:x:501:1::/home/hermie:/bin/bash

Though there are alternatives, I suggest you familiarize yourself with bash's features first, because bash is the most commonly used Linux shell. In the rest of this section, we'll go over the basics of working in the bash shell, so go ahead and log in as user hermie now and follow right along. It'll help a lot to enter the commands as you go, experiment with them on your own, and see the actual output.

For more information on the bash shell, see the bash manual.

How Do I Use the Linux Command Prompt?

Linux Commands

When you enter a command in Linux, you type a command at a prompt and then press enter. Commands can be more than one word--some require switches (which modify the command's behavior ) and/or file names (which tell the command what data to act on). Let's dissect the command shown here:

$ ls -l sample.doc

Linux Commands Are Case Sensitive

One of the most important things to remember about entering commands in any Unix environment is that case matters. In DOS, you can type DIR or dir or even DiR and get the same result, but not in Linux. Linux will be quite put out if you type LS instead of ls to list your files. With file names, you have to be even more careful, since nearly identical files (save for capitalization) can exist in the same directory, though they may have completely different contents--Cookie_Recipe and cookie_recipe would appear as distinctly different files to Linux, though they may look pretty much the same to you except for the capital letters.

The best rule to follow is this: Almost everything in Linux is lowercase, so avoid capital letters when naming your files.

Command Prompts Can Vary

When the Linux shell is ready for a command, you see a command prompt. Just as in DOS, Linux's command prompts vary. For example, when you log in as root, your default command prompt is the pound (#) sign, but if you log-in as a regula r user (like hermie), the prompt changes to a dollar sign ($).

Bash uses the different prompts to clue you in to your user privileges. Pay attention to the prompts so you don't inadvertently wipe out important files while logged in to the root account with superuser privileges, for example.

It's especially important to mind the prompts if you use the su (switch user) command, which allows you to act temporarily as the root user while you're logged in as a regular user. Watch how the prompt changes in the following example. (User input is in boldface.)

$ who am i
hermie
$ su - root
Enter password for root: xxxxxxx
# who am i
root
# exit
$ who am i
hermie

In this example, entering the command who am i tells you who the system thinks you are--hermie. Then the su - root command switches you to the root user (note the prompt change to the pound sign). The < STRONG>exit command exits the root user account and brings you back to hermie; the prompt changes back to a dollar sign. (See "Important Linux Commands" for more information on the su comma nd.)

This example used the prompt and the who am i command to show the logged-in user, but customizing your prompt is a better way to keep track of where you are.

For example, the command

PS1="\u \$ "

changes the prompt so that it displays the user name (\u), followed by the dollar sign (or pound sign, if you're a superuser). You can use other characters to insert the current time, date, or working directory (\t, \d, and \w, respectively). Here's how to use these various options:

PS1="\t \$ " yields 09:15:24 $.

PS1="\u (\d) \$ " yields hermie (Wed Nov 4) $.

PS1="\u (\w) \$ " yields hermie (/home/hermie) $.

All you're actually doing here is setting the variable PS1(prompt string number 1) to a special string of characters. The bash shell interprets the value of the PS1 variable each time it's ready to build the prompt string.

In the "Environment Variables" section, you'll learn more about special variables such as PS1 and how to set them automatically each time you log in.

Can I Use Wildcards with Linux?

Wildcards

Wildcards come in handy when you want to perform an operation on a group of files. As with DOS or Windows, if you want to delete all your files that start with "jan" and end with "txt," you can use the asterisk (*) wildcard character, as in rm j an*txt, to delete all such files (rm is the command you use to delete files).

The * character tells the shell to find any files that begin with "jan" and end with "txt" regardless of the number of characters between. It will even find files with no characters between, like jantxt. Thus, a file named jan-9 6.txt would be deleted, as would jantxt.

A more restrictive wildcard is the question mark, which matches any single character. Here are some examples of its use:

rm jan-8?.txt deletes jan-81.txt and jan-82.txt, but not jan-89b.txt

rm jan-95.??? deletes jan-95.txt and jan-95.dat, but not jan-95.db

When you use wildcards, the shell finds all matching files and expands the command you entered, so the rm jan-95.* command would be the same as typing

rm jan-95.txt jan-95.dat jan-95.db

Programmers take note: In Linux, the shell performs wildcard interpretation, and the actual commands (programs) never see the wildcard characters. This provides a convenient and common way for all Linux programs to handle wildcards. In DOS, the program (not the shell) must have the intelligence to handle wildcards. The unhappy result there is that you never know which DOS commands will accept wildcards, and each program may interpret them differently--yuck!

Can I Recall Previous Commands?

Command History and Editing

Bash remembers what commands you've recently entered so that you can recall and issue them again easily. If you press the up-arrow key, bash places the contents of the previous command on the command line. Repeatedly pressing the up or down arrow naviga tes through the command history; you can even modify the text in the recalled commands with the left and right arrows, as well as the insert and delete keys, before pressing enter to issue the recalled command.

Command Completion: Linux Can Even Read Your Mind

If you're a lazy typist, you'll love this feature. Let's say you have a directory containing the following files:

cars-are-fun
cats-are-bad
dogs-are-good
birds-have-lips

Typing rm cat and then pressing the tab key magically expands your command line to

rm cats-are-bad

matching a file in your current directory that starts with the word cat. The shell looks at what you've typed so far and then checks to see if there is a single file that starts with those characters. If there is, the shell finishes typing that file name for you. If there isn't such a file, nothing happens. If multiple files match, a beep will sound. You can then press tab twice to see all the matches, or just keep typing the filename.

You still have to press enter, but if you remember this nifty feature, you can save a lot of keystrokes and pretend that the computer is actually reading your mind!

What Are Linux File Aliases Used For?

Aliases: Create Meaningful Synonyms for Commands

Defining an alias is another way to minimize your work at the keyboard, and you can also eliminate the need to remember long, awkward commands by creating synonyms that are more meaningful to you. Here are some examples:

alias dir='ls -l'
alias dogs='find . -name "*dog*" -print'

In this example, the first alias tells bash that when you enter dir on the command line, it should actually execute the ls -l command instead. If you're a hard-core DOS user, you could use alias to create DOS-like synonyms for many Linux commands