Debian GNU/Linux : Guide to Installation and Usage
Chapter 6
rmdir is just like rm, only it’s for directories. Notice that rmdir only works on empty directories. If the directory contains files, you must delete those files first, or alternatively you can use rm -r in place of rmdir.
cd ..
This moves out of the current directory, and into its parent directory. Now you can type the following:
rmdir practice
This will delete the last remnants of your practice session.
So now you know how to create, copy, move, rename, and delete files and directories. You also learned some shortcuts, like typing simply cd to jump to your home directory, and how . and .. refer to the current directory and its parent, respectively. You should also remember the concept of the _root directory_, or /, and the alias ~ for your home directory.
5.2.2 Dot Files and ls -a
When you type ls, files beginning with a dot are not listed. Traditionally, files that contain configuration information, user preferences, and so on begin with a dot; these are hidden and out of your way while you do your day-to-day work. Sample dot files are ~/.emacs, ~/.newsrc, ~/.bashrc, ~/.xsession, and ~/.fvwmrc. These are used by Emacs, news readers, the Bash shell, the X Window system, and the fvwm window manager, respectively. It is conventional to end the dot filename with rc, but some programs don’t. There are also directories beginning with a dot, such as ~/.gimp and ~/.netscape, which store preferences for the Gimp and Netscape.
Sometimes a program will create a dot file automatically; for example, Netscape allows you to edit your preferences with a graphical dialog box and then it saves your choices. Other times you will create them yourself using a text editor; this is the traditional way to do it, but you have to learn the peculiar format of each file—inconvenient at first, but it can give you a lot of power.
To see dot files, you must use the -a option to ls. The long form of -a is -all, if you find that easier to remember. You can also use -A or -almost-all, which displays all dot files except . and ... Remember that . is the current directory, and .. is the parent of the current directory; because these are guaranteed to be in every directory, there is no real reason to list them with ls. You already know they are there.
5.3 Processes
We mentioned before that GNU/Linux is a _multitasking_ system. It can do many tasks at once. Each of these tasks is called a _process_. The best way to get a sense of this is to type top at the shell prompt. You’ll get a list of processes, sorted according to how much of the computer’s processing time they’re using. The order will continuously change before your eyes. At the top of the display, there’s some information about the system: how many users are logged in, how many total processes there are, how much memory you have and how much you’re using.
In the far left column, you’ll see the user owning each process. The far right column shows which command invoked the process. You’ll probably notice that top itself, invoked by you, is near the top of the list (because anytime top checks on CPU usage, it will be active and using CPU to do the check).
Note that in all the commands ending in “d” —such as kflushd and inetd —the “d” stands for _daemon_.
Daemon originally meant Disks And Extensions MONitor. A daemon is a non-interactive process, that is, it’s run by the system and users never have to worry about it. Daemons provide services like Internet connectivity, printing, or e-mail.
Now press u and give top your username when it asks. The u command asks to see only those processes belonging to you; it allows you to ignore all the daemons and whatever other people are doing. You might notice bash, the name of your shell. You’ll pretty much always be running bash.
Note that column two of the top display shows you the _PID_, or Process IDentification number. Each process is assigned a unique PID. You can use the PID to control individual processes (more on that later). Another useful trick is to press ? to get a list of top commands.
You may wonder about the difference between a “process” and a “program.” In practice, people use the terms interchangeably. Technically, the _program_ is the set of instructions written by a programmer and kept on disk. The _process_ is the working instantiation of the program kept in memory by Linux. But it’s not that important to keep the terms straight.
Much of your interaction with a computer involves controlling processes. You’ll want to start them, stop them, and see what they’re up to. Your primary tool for this is the _shell_.
5.4 The Shell
The _shell_ is a program that allows you to interact with your computer. It’s called a shell because it provides an environment for you to work in—sort of a little electronic home for you as you compute. (Think hermit crab.)
The simplest function of the shell is to launch other programs. You type the name of the program you want to run, followed by the arguments you want, and the shell asks the system to run the program for you.
Of course, graphical windowing systems also fill this need. Technically, Windows 95 provides a graphical shell, and the X Window system is another kind of graphical shell. But “shell” is commonly used to mean “command-line shell.”
Needless to say, the hackers who work on shells aren’t satisfied with simply launching commands. Your shell has a bewildering number of convenient and powerful features if you would like to take advantage of them.
There are countless different shells available; most are based on either the _Bourne shell_ or the _C shell_, two of the oldest shells. The original Bourne shell’s program name is sh, while csh is the C shell. Bourne shell variants include the Bourne Again Shell from the GNU project (bash, the Debian default), the Korn shell (ksh), and the Z shell (zsh). There is also ash, a traditional implementation of the Bourne shell. The most common C shell variant is tcsh (the t pays tribute to the TENEX and TOPS-20 operating systems, which inspired some of tcsh’s improvements over csh).
bash is probably the best choice for new users. It is the default and has all the features you’re likely to need. But all the shells have loyal followings; if you want to experiment, install some different shell packages and change your shell with the chsh command. Just type chsh, supply a password when asked, and choose a shell. When you next log in, you’ll be using the new shell.
5.5 Managing Processes with bash
Debian is a multitasking system, so you need a way to do more than one thing at once. Graphical environments like X provide a natural way to do this; they allow multiple windows on the screen at any one time. Naturally, bash (or any other shell) provides similar facilities.
Earlier you used top to look at the different processes on the system. Your shell provides some convenient ways to keep track of only those processes you’ve started from the command line. Each command line starts a _job_ (also called a _process group_) to be carried out by the shell. A job can consist of a single process or a set of processes in a _pipeline_ (more on pipelines later).
Entering a command line will start a job. Try typing man cp, and the cp manual page will appear on the screen. The shell will go into the background and return when you finish reading the manual page (or you can press q to quit rather than scrolling through the whole thing).
But say you’re reading the manual page, and you want to do something else for a minute. No problem. Press Ctrl-z while you’re reading to _suspend_ the current foreground job and put the shell in the foreground. When you suspend a job, bash will first give you some information on it, followed by a shell prompt. You will see something like this on the screen:
NAME cp - copy files SYNOPSIS cp [options] source --More-- [1]+ Stopped man cp $
Note the last two lines. The next to last is the job information, and then you have a shell prompt.
bash assigns a _job number_ to each command line you run from the shell. This allows you to refer to the process easily. In this case, man cp is job number 1, displayed as [1]. The + means that this is the last job you had in the foreground. bash also tells you the current state of the job—Stopped—and the job’s command line.
There are many things you can do with jobs. With man cp still suspended, try the following commands:
man ls
Starts a new job.
Ctrl-z
Suspends the man ls job; you should see its job information.
man mv
Starts yet another job.
Ctrl-z
Suspends it.
jobs
Asks bash for a display of current jobs. The result looks like this:
{$} jobs [1] Stopped man cp [2]- Stopped man ls [3]+ Stopped man mv {$}
Notice the - and +, denoting respectively the next to last and last foreground jobs.
fg
Places the last foreground job (man mv, the one with the +) in the foreground again. If you press the space bar, the man page will continue scrolling.
Ctrl-z
Re-suspends man mv.
fg %1
You can refer to any job by placing a % in front of its number. If you use fg without specifying a job, the last active one is assumed.
Ctrl-z
Re-suspends man cp.
kill %1
Kills off job 1. bash will report the job information, which will look like this:
$ kill %1 [1]- Terminated man cp $
bash is only asking the job to quit, and sometimes a job will not want to do so. If the job doesn’t terminate, you can add the -KILL[1] option to kill to stop asking and start demanding. For example:
[1] Many people use the signal number -9 instead of the signal name -KILL. However, it’s technically more portable to use the signal name.
$ kill -KILL %1 [1]- Killed man mv $
The -KILL option forcibly and unconditionally kills off the job.
In technical terms, kill simply sends a signal. By default, it sends a signal that requests termination (TERM, or signal 15) but you can also specify a signal, and signal 9 (KILL) is the signal that forces termination. The command name kill is not necessarily appropriate to the signal sent; for example, sending the TSTP (terminal stop) signal suspends the process but allows it to be continued later.
top
This brings the top display back up. Give the u command in top to see only your processes. Look in the right-hand column for the man ls and man mv commands. man cp won’t be there because you killed it. top is showing you the system processes corresponding to your jobs; notice that the PID on the left of the screen does not correspond to the job number.
You may not be able to find your processes because they’re off the bottom of the screen; if you’re using X (see Chapter 9 on page [*]), you can resize the xterm to solve this problem.
Even these simple jobs actually consist of multiple processes, including the man process and the pager more, which handles scrolling one page at a time. You may notice the more processes are also visible in top.
You can probably figure out how to clean up the remaining two jobs. You can either kill them (with the kill command) or foreground each one (with fg) and exit it. Remember that the jobs command gives you a list of existing jobs and their status.
One final note: The documentation for bash is quite good, but it is found in the Info help system rather than the man pages. To read it, type info bash. See section A.1.1 for instructions on using the info program. bash also contains a very good summary of its commands accessible by the help command. help displays a list of available topics; more information about each of them is accessible with the command help topic name. Try typing help cd, for example. This will give you details on the -P and -L arguments recognized by cd.
5.6 A Few bash Features
This section mentions just a few of the most commonly used Bash features; for a more complete discussion see Chapter 6.
5.6.1 Tab Completion
The bash shell can guess what filename or command you are trying to type and automatically finish typing it for you. Just type the beginning of a command or filename and press Tab. If bash finds a single unique completion, it will finish the word and put a space after it. If it finds multiple possible completions, it will fill out the part all completions have in common and beep. You can then enter enough of the word to make it unique and press Tab again. If it finds no completions, it will simply beep.
5.7 Managing Your Identity
Unix-like systems are multiuser, and so you have your own electronic identity as a user on the system. Type finger _yourusername_ to look at some of the information about you that’s publically available. To change the name and shell listed there, you can use the commands chfn and chsh. Only the superuser can change your login (username) and directory. You’ll notice that it says “No plan.” A “plan” is just some information you can make available to others. To create a plan, you put whatever information you want people to see in a file called .plan. To do this you’ll use a text editor; see section 8.2 on page [*]. Then finger yourself to see your plan. Others can finger you to see your plan and to check whether you’ve received new mail or read your mail.
Note that this finger information is available to the entire Internet by default. If you don’t want this, read about configuring inetd and the file /etc/services. Eventually the installation manual will describe this configuration, but for now you might try the man pages or just put nonsense in for your finger information.
6. Using the Shell
As you have been reading this book, you’ve been interacting with the shell already. The shell is the program that reads your commands and then does what you ask it to. In this chapter, you explore the shell in greater detail, with a special eye towards customizing the shell to work as you want it to.
6.1 Environment Variables
Every process has an _environment_ associated with it. An environment is a collection of _environment variables_. A variable is a changeable value with a fixed name. For example, the name EMAIL could refer to the value [email protected]. The value can vary; EMAIL could also refer to [email protected].
Because your shell is a process like any other, it has an environment. You can view your shell’s environment by entering the printenv command. Figure 6.1 has some sample output from printenv. On your system, the output will be different but similar.
Environment variables are one way to configure the system. For example, the EDITOR variable lets you select your preferred editor for posting news, writing e-mail, and so on.
Setting environment variables is simple. For practice, try customizing your shell’s prompt and your text file viewer with environment variables. First, let’s get a bit of background information.
man less
This command lets you view the online manual for the less command. In order to show you the text one screenful at a time, man invokes a pager that shows you a new page of text each time you press the space bar. By default, it uses the pager called more.
Go ahead and glance over the man page for less, which is an enhanced pager. Scroll to a new page by pressing space; press q to quit. more will also quit automatically when you reach the end of the man page.
export PAGER=less
After reading about the advantages of less, you might want to use it to read man pages. To do this, you set the environment variable PAGER.
The command to set an environment variable within bash always has this format:
Illustration: Figure 6.2: Changing the prompt
export NAME=value
export means to move the variable from the shell into the environment. This means that programs other than the shell (for instance, a file viewer) will be able to access it.
echo $PAGER
This is the easiest way to see the value of a variable. $PAGER tells the shell to insert the value of the PAGER variable _before_ invoking the command. echo echoes back its argument: in this case, it echoes the current PAGER value, less.
man more
Displays the more manual. This time, man should have invoked the less pager.
less has lots of features that more lacks. For example, you can scroll backward with the b key. You can also move up and down (even sideways) with the arrow keys. less won’t exit when it reaches the end of the man page; it will wait for you to press q.
You can try out some less-specific commands, like b, to verify that they don’t work with more and that you are indeed using more.
unset PAGER
If you don’t want to specify a pager anymore, you can unset the variable. man will then use more by default, just as it did before you set the variable.
echo $PAGER
Because PAGER has been unset, echo won’t print anything.
PS1=hello:
Just for fun, change your shell prompt. $ should now change; see Figure 6.2 for details.
export is not necessary, because you’re changing the shell’s own behavior. There’s no reason to export the variable into the environment for other programs to see. Technically, PS1 is a _shell variable_ rather than an environment variable.
If you wanted to, you could export the shell variable, transforming it into an environment variable. If you do this, programs you run from the shell can see it.
6.2 Where Commands Reside: The PATH Variable
When you type a command into the shell, it has to find the program on your hard disk before executing it. If the shell had to look all over the disk, it would be very slow; instead, it looks in a list of directories contained in the PATH environment variable. This list of directories makes up the shell’s _search path;_ when you enter a command, it goes through each one in turn looking for the program you asked to run.
You may need to change the PATH variable if you install programs yourself in a non-standard location. The value of PATH is a colon-separated list of directories. The default value on Debian systems is as follows:
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games / / / / This value is defined in the file /etc/profile and applies to all users. You can easily change the value, just as you can change any environment variable. If you type the command ls, the shell will first look in /usr/local/bin; ls isn’t there, so it will try /usr/bin; when that /fails, it will check /bin. There it will discover /bin/ls, stop its search, and execute the program /bin/ls. If /usr/bin/X11/ls existed (it doesn’t, but pretend), it would be ignored.
You can see which ls the shell is going to use with the type command. type ls will give you the answer /bin/ls. Try it yourself.
Try asking where type itself resides:
$ type type type is a shell builtin
type isn’t actually a program; it’s a function provided by the shell. However, you use it just like an external program.
There are a number of commands like this; type man builtins to read the man page describing them. In general, you don’t need to know whether a command is a builtin or a real program; however, builtins will not show up in the output of ps or top because they aren’t separate processes. They’re just part of the shell.
6.3 Configuration Files
Many applications on Linux systems allow you to alter how they behave at certain times by altering files containing configuration information. These configuration files may contain application start-up information, run-time settings and application shutdown settings. In general, a configuration filename is based on the name of the application for which it contains settings. Such a naming convention allows you to more readily determine which configuration file contains settings for a given application.
6.3.1 System-Wide Versus User-Specific Configuration
It’s important to remember that there are two different kinds of configurations on a Debian system. _System-wide configuration_ affects all users. System-wide settings are made in the /etc directory, so you generally must be root in order to change system-wide settings. You might configure the way the system connects to the Internet, for example, or have web browsers on the system always start on the company home page. Since you want these settings to apply to all users, you make the changes in /etc. Sample configuration files in /etc include /etc/X11/XF86Config, /etc/lynx.cfg, and /etc/ppp/options. In fact, nearly all the files /in /etc are configuration files.
_User configuration_ affects only a single user. Dotfiles are used for user configuration. For example, the file ~/.newsrc stores a list of which USENET (discussion group) articles you have read and which groups you subscribe to. This allows news readers such as trn or GNUS to display unread articles in the groups you’re interested in. This information will be different for every user on the system, so each user has his own .newsrc file in his home directory.
6.4 Aliases
If you use the same command often, you might get tired of typing it. bash lets you write shorter _aliases_ for your commands.
Say you always use the -almost-all and -color=auto options to ls. You quickly get tired of typing ls -almost-all -color=auto. So you make an alias:
alias myls=’ls -almost-all -color=auto’
Now you can type myls instead of the full command. To see what myls really is, run the command type myls. To see a list of aliases you’ve defined, simply type alias on a line by itself.
6.5 Controlling Input and Output
Throughout your experiences with Linux, you will most likely find that manipulating application input and output can be a very powerful thing to do. This section describes some of the things that controlling input and output can do for you.
6.5.1 stdin, stdout, Pipelines, and Redirection
Every process has at least three connections to the outside world. The _standard input_ is one source of the process’s data; the _standard output_ is one place the process sends data; and the _standard error_ is a place the process can send error messages. (These are often abbreviated stdin, stdout, and stderr.)
The words “source” and “place” are intentionally vague. These standard input and output locations can be changed by the user; they could be the screen, the keyboard, a file, even a network connection. You can specify which locations to use.
When you run a program from the shell, usually standard input comes from your keyboard, and standard output and error both go to your screen. However, you can ask the shell to change these defaults.
For example, the echo command sends it output to standard output, normally the screen. But you can send it to a file instead with the _output redirection operator_, >. For example, to put the word “Hello” in the file myfile, use this command:
echo Hello > myfile
Use cat or your text file pager (more or less) to view myfile’s contents; see Figure 6.3.