Red Hat Linux: Basic Linux Commands And UtilitiesBasic Linux Commands and Utilities
Most Linux commands are very flexible. When you enter a Linux command,
there are several ways to tailor the basic command to your specific needs. We
will look at the two main ways used to modify the effect of a command:
A simple way to picture what a Linux command does is to imagine that it's a
black box that is part of an assembly line. Items come down a conveyor belt,
enter the black box, get processed in some way, come out of the black box, and
are taken away on another conveyor belt. Command options let you fine-tune the
basic process happening inside the black box. Command redirection lets you
specify which conveyor belt will supply the black box with items and which
conveyor belt will take away the resulting products. Once you understand how redirection and command options work, you will be
able to (at least in principle) use any Linux or UNIX command. This is because
UNIX was based on a few simple design principles. Commands, therefore, should
work in consistent ways. Of course, UNIX has grown and changed over the years,
and the design principles can sometimes get buried under all the changes. But
they still make up the foundation, so that UNIX-based systems such as Linux are
quite coherent and consistent in how they work.
Command Options You can use command options to fine-tune the actions of a Linux
command. Quite often, a Linux command will do almost—but not quite—what you want
it to do. Instead of making you learn a second command, Linux lets you modify
the basic, or default, actions of the command by using options. The ls command is an excellent, and useful, example of a command that has a
great many options. The ls command lists the files found on the Linux system's
hard drive. This sounds simple enough, doesn't it? Try entering the command darkstar:~$ ls Well, nothing much seemed to happen. Now try typing ls -a. Type it exactly as listed. The space between ls and -a
is necessary, and there must be no space between the - and the a. darkstar:~$ ls -a What you have done is modified what ls does by adding a command option—in
this case, -a. By default, ls lists only files whose names don't begin with a
period. However, -a tells ls to list all files, even ones that begin with a
period. (These are usually special files created for you by Linux.) At present,
all the files in your directory start with a period, so ls by itself does not
list any files; you must add -a to see the files you have at present. The ls command has many more options. You can use more than one option at a
time. For example, try typing ls -al: darkstar:~$ ls -al You now get a listing with many more details about the files. The l option can be used by itself; ls -l will give
detailed descriptions of files that don't begin with a period. Sometimes
filenames are so long they don't fit on a single line. Linux simply wraps the
remainder to the next line.
By default, ls lists files in alphabetical order. Sometimes you might be more
interested in when a file was created or last modified. The t option tells ls to
sort files by date instead of alphabetically by filename, showing the newest
files first. Therefore, typing ls -alt gives darkstar:~$ ls -alt The r option tells ls to produce a reverse output. This is often used with
the t option. The following is an example of what you might get if you entered
ls -altr: darkstar:~$ ls -altr Many other options can be used with ls, although we have now tried the most
commonly used ones. The important thing to remember is that you can usually
customize a Linux command by using one or more command options.
Other Parameters Linux commands often use parameters that are not actual command
options. These parameters, such as filenames or directories, are not preceded by
a dash. For instance, by default ls lists the files in your current directory. You
can, however, tell ls to list the files in any other directory simply by adding
the directory to the command line. For instance, ls /bin will list everything in
the /bin directory. This can be combined with command options, so that ls -l
/bin gives you detailed listings of the files in /bin. Try this. You will be
impressed by the number of files in the /bin directory! You can also specify ls to list information about any particular file by
entering its filename. For instance, ls -la .lessrc gives detailed information
only about the .lessrc file. Many Linux commands let you specify which file or directory they are to
act upon, as we saw with the example ls -l /bin earlier. You can also "pipe" the output from a command so that it becomes another
command's input. This is done by typing two or more commands separated by the |
character. (This character normally is found on the same key as the \ character.
You must hold down the Shift key or you will get \ instead of |). The |
character means "Use the output from the previous command as the input for the
next command." Therefore, typing command_1|command_2 does both commands, one
after the other, before giving you the results. Using our assembly-line metaphor, we are processing items through two black
boxes instead of just one. When we use piping, it's like hooking up the first
command's output conveyor belt to become the input conveyor belt for the second
command.
You will have noticed that the output of ls -l /bin is many lines long, so
that much of the information scrolls off the screen before you can read it. You
can pipe this output to a formatting program called more, which displays
information in screen-sized chunks. When you enter ls -l /bin | more, you will
see the following: darkstar:~$ ls -l /bin | more The —More— at the bottom of the screen tells you that there's more text to
come. To go to the next screen of text, press the spacebar. Every time you press
the spacebar, more displays another screenful of text. When the last screenful
of text has been displayed, more returns you to the Linux prompt.
Another thing you can do in Linux is to send output to a file instead of the
screen. There are many different reasons why you might want to do this. You
might want to save a "snapshot" of a command's output as it was at a certain
time, or you might want to save a command's output for further examination. You
might also want to save the output from a command that takes a very long time to
run, and so on. To send output to a file, use the > symbol (found above the period on your
keyboard). For instance, you can place the output of the ls -l /bin command into
a file called test by typing ls -l /bin > test. Again, spaces around > are
optional and not strictly necessary, but they do make the command much more
readable. If you now do an ls or ls -l, you will see that you've created a new file
called test in your own directory. To see the contents of a file, you can again use the more command. Just
specify the name of the file you want to look at. In this case, you would type
more test.
You can specify that you want to add your output to the end of the file,
rather than replace the file's contents, by using >>. Type who >> test to add
the output of the who command to the end of the text already in the file test.
You can examine the results by using either more or less and paging through
to the end of the file, or by using the Linux command tail, which displays the
last few lines of the specified file. In this case, you would type tail test to
see the last few lines of the file test. Try using tail! Notational Conventions Used to Describe Linux Commands There is a set of accepted notational conventions used to describe, in
a concise and consistent way, the correct syntax for any given Linux command.
This specifies what options or parameters you must use, what options or
parameters you can use or not use, and so on. Sometimes this set of conventions
is used to give a complete and exhaustive listing of a command's syntax, showing
every possible command and parameter. Sometimes it is used to make a particular
example more general and the command's basic usage clearer. If you remember the following six basic rules, you will be able, in
principle, to understand the syntax of any Linux or UNIX command.
Online Help Available in Linux Linux has help facilities available online. If you forget the exact use
of a command, or you're looking for the right command to use, the answer might
be available straight from Linux. The two help facilities we will try out are
the bash shell's help command, and the man command, which is available on almost
all UNIX systems, including Linux.
The Linux Man Pages The "man" in "man pages" stands for "manual." (As usual, the creators
of UNIX shortened a long but descriptive word to a shorter, cryptic one!) Typing
man <command> lets you view the manual pages dealing with a particular command.
Try typing man passwd to see what the Linux manual has to say about the
passwd command. The general layout of a man page is as follows: COMMAND(1) Linux Programmer's Manual COMMAND(1) The man page for passwd is actually quite understandable. Be warned, however,
that man pages are often written in a very formal and stylized way that
sometimes bears little resemblance to English. This is done not to baffle
people, but to cram a great deal of information into as short a description as
possible. For example, try man ls. Notice how many options are available for ls and how
long it takes to explain them! Although it can take practice (and careful reading!) to understand man pages,
once you get used to them, the first thing you'll do when you encounter a
strange command is call up the man page for that command. Finding Keywords in Man Pages Sometimes you know what you want to do, but you don't know which
command you should use to do it. You can use the keyword option by typing man -k
<keyword>. The man program will return the name of every command whose name
entry (which includes a very brief description) contains that keyword. For instance, you can search on manual: darkstar:~$ man -k manual You have to be careful to specify your keyword well, though! Using directory
as your keyword isn't too bad, but using file will give you many more entries
than you will want to wade through.
The bash Shell help Facility When you type a command at the prompt, the shell program takes what
you've written, interprets it as necessary, and passes the result to the Linux
operating system. Linux then performs the actions requested of it. Many Linux
commands require Linux to find and start up a new program. However, the shell
itself can perform a number of functions. These functions can be simple,
often-used commands, so that the overhead of starting up separate programs is
eliminated, or they can be facilities that make the shell environment friendlier
and more useful. One of these facilities is the help command, which provides
information on the bash shell's built-in functions. Type help at the prompt. You will see at least some of the following: GNU bash, version 1.14.6(1) You will have to pipe the output of help to more (help | more) to keep the
first part from scrolling off your screen. Wildcards * and ? In many a late-night card game, jokers are shuffled into the deck. The
jokers are wildcards that can become any card of your choice. This is obviously
very useful! Linux has wildcards also. They are, if anything, more useful than
jokers in a card game. Linux has several wildcards. Wildcards are used as a convenient and powerful
shortcut when specifying files (or directories) that a command is to operate on.
We will briefly look at the two most popular wildcards: * and ?. The most commonly used wildcard is *, which stands in for any combination of
one or more characters. For example, c* will match all filenames that begin with
c. You can see this for yourself by typing ls /bin/c*. What happens if you type ls /bin/c*t? How about ls /bin/*t? The ? wildcard is more restrictive than *. It only stands in for any one
character. You can see this by comparing ls/bin/d* with ls/bin/d?.
Be very careful when using wildcards with dangerous commands, such as the ones used to permanently delete files! A good check is to run ls with the wildcards you plan to use and examine the resulting list of files to see if the wildcard combination did what you expected it to do. Also double-check that you typed everything correctly before pressing the Enter key! Environment Variables When you log in, Linux keeps a number of useful data items in the
background ready for the system to use. The actual data is held in something
called an environment variable, whose name is often descriptive or mnemonic. In
fact, this is no different from the way you and I remember things. We know that
there always is a piece of information called "day of the week" (the environment
variable); however, we change the data in this variable, from Monday to Tuesday
to Wednesday, and so on, as days go by. To see the list of exported environment variables, type env. The environment
variable's name is on the left, and the value held by the variable is on the
right. The most important variable to note is the PATH, whose value is your search
path. As we will see in the next chapter, when you type a command, Linux will
search every place listed in your search path for that command. A longer list of environment variables, consisting of several new variables
in addition to the ones you saw earlier, is displayed by the command set. The
new variables are local: they have not been marked for export. You can think
of local variables as items of information you need for only a certain time or
location. For instance, remembering the variable "what-floor-am-I-on" becomes an
unnecessary piece of information once you leave the building! Processes and How to Terminate Them
In the previous chapter, we learned about the who command, which shows
you the usernames of everyone who is logged into the system. The who program
actually gets its information from the Linux system, which maintains and updates
the list of the system's current users. In fact, Linux keeps much more detailed records about what is happening on
the system than just who is logged in. Because Linux is a multitasking system,
in which many programs or program threads may be running simultaneously, Linux
keeps track of individual tasks or processes. Although these processes are usually well-behaved and well-managed by Linux,
sometimes they might go out of control. This can happen if a program hits a bug
or a flaw in its internal code or supplied data, or if you accidentally enter
the wrong command or command option. Being able to identify these misbehaving processes, and then being able to
terminate or kill them, is an essential piece of knowledge for all Linux/UNIX
users. (Obviously the world was a less kind and gentle place when the kill
command was developed and named.) When you are your own system administrator, as
in our case, it's doubly important! To find out what processes are running, we use the ps command. ps
stands for "process status," not the "post script" you would write at the end of
a letter. Typing ps by itself gives you a concise listing of your own processes: darkstar:~$ ps The information in the first column, headed PID, is important. This is the
Process ID number, which is unique, and which Linux uses to identify that
particular process. You must know a process's PID to be able to kill it. The TTY column shows you which terminal the process was started from. The STAT column gives the status of the process. The two most common entries
in the status column are S for sleeping and R for running. A sleeping process is
one that isn't currently active. However, don't be misled. A sleeping process
might just be taking a very brief catnap! In fact, a process might switch
between sleeping and running many times every second. The TIME column shows the amount of system time used by the process. Clearly,
neither of our processes are taking up any appreciable system time! Finally, the COMMAND column contains the name of the program you're running.
This will usually be the command you typed at the command line. However,
sometimes the command you type starts one or more child processes, and in this
case, you would see these additional processes show up as well, without ever
having typed them yourself. Your login shell will have a - before it, as in
-bash in the previous example. This helps to distinguish this primary shell from
any shells you might enter from it. These will not have the - in front.
One useful option with ps is u. Although it stands for "user," as in "List
the username as well," it actually adds quite a few more columns of information
in addition to just the username: darkstar:~$ ps -u In addition to the username in the USER column, other interesting new items
include %CPU, which shows you what percentage of your computer's processing
power is being used by the process, and %MEM, which shows you what percentage of
your computer's memory is being used by the process. If you want to see all processes running on the system, and not just the
processes started by your own username, you can use the a command option. (The
root login sees everyone's processes automatically and does not have to use a,
so root can get the following output by simply typing ps.) darkstar:~$ ps -a As you can see, quite a few "other" processes are happening on the system! In
fact, most of the processes we see here will be running whether or not anyone is
actually logged into the Linux system. All the processes listed as running on
tty psf are actually system processes, and are started every time you boot up
the Linux system. Processes of the form /sbin/agetty 38400 tty6 are login
processes running on a particular terminal waiting for your login. It can be useful to combine the a and u options (if you're not root). darkstar:~$ ps -au A more technical l option can sometimes be useful: darkstar:~$ ps -l The interesting information is in the PPID column. PPID stands for "Parent
Process ID"—in other words, the process that started the particular process.
Notice that the ps -l command was started by -bash, the login shell. In other
words, ps -l was started from the command line. Notice also that the PPID for
the login shell is PID 1. If you check the output from ps -au shown previously,
you will see that the process with PID of 1 is init. The init process is the one
that spawns, or starts, all other processes. If init dies, the system crashes!
The Process Termination Command kill The kill command is used to terminate processes that can't be stopped
by other means.
Linux keeps ordinary users (as opposed to root) from killing other users'
processes (maliciously or otherwise). For instance, if you are an ordinary user
and you try to kill the init process, which always has PID=1, you will see darkstar:~$ kill 1 Actually, not even root can kill the init process, although there is no error
message. The init process is one of those "unkillable" processes discussed
earlier, because it's such a key process. That's all for the best! Usually, when you want to temporarily become a different user, you will
simply switch to another virtual terminal, log in as the other user, log out
when you're done, and return to your "home" virtual terminal. However, there are
times when this is impractical or inconvenient. Perhaps all your virtual
terminals are already busy, or perhaps you're in a situation (such as logged on
via a telephone and modem) in which you don't have virtual terminals available.
In these cases, you can use the su command. "su" stands for "super user." If
you type su by itself, you will be prompted for the root password. If you
successfully enter the root password, you will see the root # prompt, and you
will have all of root's privileges. You can also become any other user by typing su <username>. If you are root
when you type su <username>, you are not asked for that user's password since in
principle you could change the user's password or examine all the user's files
from the root login anyway. If you are an "ordinary" user trying to change to
another ordinary user, you will be asked to enter the password of the user you
are trying to become.
The grep Command "What on earth does grep mean?" you ask. This is a fair question. grep must be the quintessential UNIX acronym,
because it's impossible to understand even when it's spelled out in full! grep stands for Global Regular Expression Parser. You will understand the use
of this command right away, but when "Global Regular Expression Parser" becomes
a comfortable phrase in itself, you should probably consider taking a vacation.
What grep does, essentially, is find and display lines that contain a pattern
that you specify. There are two basic ways to use grep. The first use of grep is to filter the output of other commands. The general
syntax is <command> | grep <pattern>. For instance, if we wanted to see every
actively running process on the system, we would type ps -a | grep R. In this
application, grep passes on only those lines that contain the pattern (in this
case, the single letter) R. Note that if someone were running a program called
Resting, it would show up even if its status were S for sleeping, because grep
would match the R in Resting. An easy way around this problem is to type grep "
R ", which explicitly tells grep to search for an R with a space on each side.
You must use quotes whenever you search for a pattern that contains one or more
blank spaces. The second use of grep is to search for lines that contain a specified pattern in a specified file. The syntax here is grep <pattern> <filename>. Be careful. It's easy to specify the filename first and the pattern second by mistake! Again, you should be as specific as you can with the pattern to be matched, in order to avoid "false" matches. |