~ CHAPTER 6: THE COMMAND LINE ~
Over time, it’s been UNIX command-line options that have given the system its power and
flexibility.
More experienced users, however, find that it is difficult for a GUI to present all of the
available options.
NOTE: For this chapter, we assume that you are logged in as root user and have started the
X Windows environment. If you are using the GNOME environment (which is the default
behavior of the latest versions of Red Hat Linux) you can start an “X terminal.” Click the
NOME footprint in the lower-left corner of your screen, select the Utilities, and then select
Color Xterm. All of the commands you enter in this chapter should be typed into the windows
that appears after color Xterm is selected.
An Introduction to BASH
The parameters for a user’s password entry is their login shell, which is the first program that
runs when a user logs in to a workstation.
A shell is simply a program that provides an interface to the system. The Bourne Again Shell
(BASH) in particular is a command-line-only interface containing a handful of built-in commands,
the ability to launch other programs, and the ability to control programs that have been launched
from it (job control). Think of it as a command.com.
Job Control
When working in the BASH environment, you can start multiple programs from the same
prompt. Each program is a job. Whenever a job is started, it takes over the terminal.
Not all programs require this kind of terminal control, however. Some, including programs that
interface with the user through the X Window System, can be instructed to give up terminal
control and allow BASH to represent a user prompt, even through the invoked program is
still running.
Immediately after you press ENTER, bash will present a prompt. This is called backgrounding
the task.
At any time you can find out how many jobs BASH is tracking by typing in jobs.
To run a job in the background, type: bg^number
To run a job number in the foreground, type: fg^number
======================================================================
linux6.html PAGE
2 2002/07/14
Environment Variables
Some environment variables have special meanings to the shell, but there is nothing stopping
you from defining your own and using them for your own needs.
Printing Environment Variables: Printenv
Setting Environment Variables: variable = value eg) FOO=BAR
Unsetting Environment Variables: unset^FOO
NOTE: This section assumed that you are using BASH. There are many other shells to choose
from; the most popular alternatives are C-Shell (csh) and its brother Turbo C-Shell (tcsh), which
use different mechanisms for getting and setting environment variables.
Pipes
Pipes are a mechanism by which the output of one program can be set as the input to another
program.
Grep utility. The grep utility, given a stream of input, will try to match the line with the parameter
supplied to it and display only matching lines. For example, if we were looking for all environment
variables containing the string “OSTYPE”, we could enter this command:
[root@ford^/root]#printenv^|^grep^”OSTYPE”
Redirection
Through redirection, we can take the output of a program and have it automatically sent to a file.
For example, here is the command to collect the output of a directory listing into a file called
/tmp/directory_listing:
[root@ford^/root]#ls^>/tmp/directory_listing
Command-Line Shortcuts
One of the difficulties in moving to a command-line interface, especially from command-line tools
such as command.com, is working with a shell that has a good number of shortcuts.
======================================================================
linux6.html PAGE
3 2002/07/14
Filename Expansion
Under UNIX-based shells such as BASH, wildcards on the command line are expanded before
being passed as a parameter to the application.
Wildcards * asterisks and ? and you can use the \ character.
Environment Variables as Parameters
Under BASH, you can use environment variables as parameters on the command line.
For example, issuing the parameter $F00 will cause the value of the FOO environment variable
to be passed rather than the string “$FOO”.
Multiple Commands
Under BASH, multiple commands can be executed on the same line by separating the commands
with semicolons (;). For example, to execute this sequence of commands on a single line:
ls^-l^;cat^/etc/passwd
Backticks
You can take the output of one program and make it the parameter of another program. Sound
bizarre? Well, time to get used to it—this is one of the most useful and innovate features available
in all UNIX shells.
Backticks (`) allow you to embed commands as parameters to other commands. You’ll see this
technique used often in this book to take a number of sitting in a file and pass the number as a
parameter to the kill command. A typical instance of this occurs when the DSN server, named,
needs to be killed.
Using backticks, however we can combine the steps into one and do it in a way that can be
automated. The backticks version would look like this:
Documentation Tools
Linux comes with two superbly useful tools for making documentation accessible: man and info
TIP: Many Linux distributions also include a great deal of documentation in the /usr/doc or
/usr/share/doc directory.
======================================================================
linux6.html PAGE
4 2002/07/14
The Man Command
The man command has several sections and they are numbered from 1-8.
For example if you type in man^3^printf, you will get the manual for C Library calls. Page 119
The textinfo System
Another common form of documentation is texinfo. To read the texinfo documents on a specific
tool or application, invoke the info with the parameter specifying the tool’s name. For example:
info^emac.
File Listings, Ownerships, and Permissions
Managing files under Linux is different than managing files under Windows NT/2000, and
radically different from managing files under Windows 95/98.
Listing Files: ls
The ls command is used to list all the files in a directory. There are over 26 options. The most
common are:
–l Long listing. In addition to the filename, shows the size date/time, permissions, ownership,
and group information.
–a. All files. Inlcudes hidden files. Names of hidden files begin with a period.
–1 Single common listing.
–R Recursively lists all files and subdirectories.
File and Directory Types
Under Linux, almost everything is abstracted to a file. Instead of having to communicate directly
with device drivers, special files are used as a bridge.
Normal Files. They contain data or executables.
Directories. Directory files list the location of other files, some of which may be other directories.
Hard Links. Each file in the Linux file system gets its own i-node.
NOTE: a hard link cannot exist between two files on separate partitions. This is because the hard
link refers to the original file by i-node, and a file’s I-node may differ among file systems.
======================================================================
linux6.html PAGE
5 2002/07/14
Symbolic Links: Points to another file by its name.
Block Devices: are used to interface with devices such as disks.
Character Devices: Are special files that allow you to access devices through the file system.
Named Pipes: Are special types of file that allows for interprocess communication.
Change Ownership: chown. Command allows you to change the ownership of a file to
someone else.
Change Group: chgrp. Command-line utility lets you change the group settings of a file. It works
much like chown.
Change Mode: chmod. Permissions are divided into four parts.
Letter Permissions Value
R Read 4
W Write 2
X Execute 1
Example 600 = R + W + blank or 4 + 2 + 0 = 600
*** See the chart
page 125 ***
File Management and Manipulation
Copy files: cp. Switches –f (force) and –i(interactive)
Move files: mv. Switches –f (force) and –i (interactive)
Link Files: In. You can establish hard links and soft links.
Find a File: find. You can search for files based on various criteria.
The –exe Parameter. It allows find to execute a command on any file it finds.
Convert and Copy a File: dd
The dd command reads the content of a file and sends it to another file.
======================================================================
linux6.html PAGE
6 2002/07/14
File Compression: gzip
In the original distribution of UNIX, the tool to compress files was appropriately called compress.
Even better, gzip consistently achieves better compression ratios than compress does.
Make Special Files: mknod
The mknod command can also be used to create name pipes.
b for block,
c for character
p for named pipe.
Create a Directory: mkdir
In Linux is identical to the same command in other UNIXs, as well as in MS-DOS.
rmdir. Similar to MS-DOS. Removes a directory.
Show Present Working Directory: pwd
If you sit down to an already logged in workstation and you do not know the path type in pwd.
This gives you the current working directory.
Tape Archive: tar
The tar program combines multiple files into a single large file. Tar is able to read and write to
devices in much the same way dd can, thus making tar a good tool for backing up to tape devices.
–c Creates a new archive.
–t View the contents of an archive.
–x Extract the contents of an archive.
–f Specify the name of the file in which the archive is located.
–v Be verbose during operations.
–z Assume the file is already compressed with gzip.
Concatenate Files:
cat
The cat program fills an extremely simple role: to display files. More create things can be done
with it, but nearly all of its usage will be in the form of simply displaying the contents of text files –
much like the type command under DOS.
======================================================================
linux6.html PAGE
7 2002/07/14
Display a File One Screen at a time: more
To view the /etc/passwd file one screen at a time, use this command:
more^/etc/passwd
To view the directory listing generated by the ls command one screen at a time, enter this:
ls^ |^m
Disk Utilization: du
The du command allows you to determine the disk utilization on a directory-by-directory basis.
–c. Produces a grand total at the end of the run.
–h Prints sized in human readable format.
–k. Prints sizes in kilobytes rather than block sizes.
–s Summarize. Print only one output for each argument.
Whereis
The whereis tool searches your path and displays the name of the program and its absolute
directory, the source file and the man page for the command.
Disk Free: df
The df program displays the amount of free space, partition by partition.
Sync
Type this command to ensure that the disk cache has been flushed:
sync^;^sync
Process Manipulation
Processes that overstep their memory allocation are immediately killed. This method of handling
processes has been a major contributor to the stability of UNIX systems: User applications
cannot corrupt other user programs or the operating systems.
======================================================================
linux6.html PAGE
8 2002/07/14
List Processes: ps
The ps command lists all the processes in a system, their state, size, named owner, CPU time,
wall clock and much more.
Show an Interactive List of Processes: top
The top command is an interactive version of ps. Instead of giving a static view of what is going
on, top refreshes the screen with a list of processes every 2-3 seconds.
The top program’s main disadvantage is that it’s a CPU hog.
chmod^0700^/usr/bin/top
Send a Signal to a Process:
kill
The operating system, by default, supplies each process a standard set of signal handlers to
deal with incoming signals.
From a system administrator’s standpoint, the most important handler is for signals number
9 and 15, kill process and terminate process, respectively.
When kill is invoked, it requires at least one parameter: the process identification number
(PID) as derived from the ps command.
Signals
The optional parameter available for kill is –n, where the n represents a signal number.
As system administrator, we are most interested in the signals 9(kill) and 1 (hang up).
Security Issues:
The power to terminate a process is obviously a very powerful one, making security
precautions is important.
Other Tools:
Uname. This tools is also helpful for script writers, because it allows them to change the
path of a script based on the system information.
======================================================================
linux6.html PAGE
9 2002/07/14
who. Allows users to log in to other user’s machines or special server, and you can see
who is logged in.
w. A variation of Who.
su. Switch user. You can remain logged in and just switch users.
telnet. Once you run the command you will get a login prompt from that machine.
.rhosts file. Needed before you can telnet to another machine.
mail. You can view your e-mail.
pine. Takes control of your terminal windows and makes the display look much nicer.
pinemutt. Is much less menu-oriented. Editor has been around since UNIX-based
systems in the 1970s.
emacs. Similar to notepad in terms of its interfaces. And it has a GUI.
joe. Another editor it is simple to use.
pico. Another editor easy to use.
Summary: