-->
Previous Table of Contents Next


Chapter 7
Basic Linux Commands

by Ed Treijs and Tim Parker

In This Chapter
•   How Linux commands work
•   Notational conventions used to describe Linux commands
•   Online help available in Linux
•   The Linux man pages
•   Wildcards: * and ?
•   Environment variables
•   Processes and how to terminate them
•   Becoming someone else: the su command
•   The grep command

How Linux Commands Work

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:

  Specifying or redirecting a command’s input and output
  Using command options

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. This analogy seems simple, but is a surprisingly effective way of understanding some complex concepts of UNIX and Linux.

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 is based on a few simple design principles. The most important of these principles for our purposes is that commands are simple and single-purpose, as well as flexible about where they get information from and where to put it when they’ve finished. All Linux commands, therefore, should work in consistent ways. Of course, UNIX has evolved over the 35 years or so it has been around, and 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 surprisingly coherent and consistent in how they work.


Tip:  
Pressing Ctrl+U at any point, right up to before you press Enter, lets you clear everything you’ve typed on the command line. You can use this whenever you spot an error at the very beginning of your typing or when you decide you don’t want to run a particular command after all. You can also use the Backspace key to “back up” by erasing characters (in fact, it can be almost a reflex action), but it’s usually faster to just erase the whole command line and start again. Alternatively, you can use the Ctrl+C sequence to cancel whatever you are typing and display a new shell prompt.

Perhaps the most powerful keys to use at the command prompt are the arrow keys in those versions of the shell that support them. The left and right arrows move the cursor non-destructively. If you make a typo early in the line, you can left-arrow your way to the character and type in a correction. Additionally, the up and down arrows enable you to jump through a list of the last several commands used (similar to DOS’s DOSKEY utility).


Command Options

You can use command options to fine-tune the actions of a Linux command. Quite often, a Linux command does 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. A simple example is when you want to sort a list of names into order. Linux has a sort command that can do that for you, but maybe you want to reverse the order of sorting so Z comes before A. Instead of having a backward sort command, a single letter option reverses the sort order. Some commands have many options, some have a few.

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 following command:


darkstar:~$ ls

darkstar:~$

Well, nothing much seems to happen in this case, and most systems respond like this if you’re using a brand-new user login.

Now try typing ls -a. Type it exactly as shown. The space between ls and -a is necessary, and there must be no space between the - and the a.


darkstar:~$ ls -a

./       .bash_history .kermrc    .lessrc

Here you have 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. Because Linux can’t hide files the way some operating systems can, it treats any file starting with a period as a special configuration file that doesn’t appear with the usual list commands.) 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 all of 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

total 10

drwxr-xr-x  3 fido   users    1024 Dec 21 22:11 ./

drwxr-xr-x  6 root   root     1024 Dec 14 01:39 ../

-rw-r--r--  1 fido   users     333 Dec 21 22:11 .bash_history

-rw-r--r--  1 fido   users     163 Dec  7 14:31 .kermrc

-rw-r--r--  1 fido   users      34 Jun  6 1993  .less

-rw-r--r--  1 fido   users     114 Nov 23 1993  .lessrc

drwxr-xr-x  2 fido   users    1024 Dec  7 13:36 .term/


Previous Table of Contents Next