What Is the Shell?
When
we speak of the command line, we are
really referring to the shell. The shell is a
program that takes keyboard commands and
passes them to the operating system to carry
out. Almost all Linux distributions supply a shell program
from the GNU roject called bash. The name is
an acronym for bourne-again shell,
a reference to the fact
that bash is an enhanced replacement for sh, the original
Unix shell program written by Steve Bourne.
Terminal Emulators
When
using a graphical user interface (GUI), we need another program called
a terminal emulator to interact with the shell. If we look through our desktop
menus, we will probably find one. KDE uses konsole, and GNOME uses
gnome-terminal, though it’s likely called simply Terminal on your menu. A
number of other terminal emulators are available for Linux, but they all basically
do the same thing: give us access to the shell. You will probably develop
a preference for one or another terminal emulator based on the number
of bells and whistles it has.
Understanding the File System Tree
Like
Windows, a Unix-like operating system such as Linux organizes its files in
what is called a hierarchical directory structure. This means they are organized in
a tree-like pattern of directories (sometimes called folders in
other systems),
which may contain files and other directories. The first directory in
the file system is called the root
directory. The root directory contains files and
subdirectories, which contain more files and subdirectories, and so on.
Note
that unlike Windows, which has a separate file system tree for each
storage device, Unix-like systems such as Linux always have a single file system
tree, regardless of how many drives or storage devices are attached to the
computer. Storage devices are attached (or more correctly, mounted)
at various
points on the tree according to the whims of the system administrator, the
person (or people) responsible for the maintenance of the system.
The Current Working Directory
Most
of us are probably familiar with a graphical file manager that represents the
file system tree, Notice that the tree is usually shown upended, that is, with
the root at the
top and the various branches descending below. However,
the command line has no pictures, so to navigate the file system
tree, we need to think of it in a different way. Imagine
that the file system is a maze shaped like an upside-down tree
and we are able to stand in the middle of it. At any given time, we are inside
a single directory, and we can see the files contained in the directory and
the pathway to the directory above us (called the parent directory)
and any
subdirectories below us. The directory we are standing in is called the current working directory. To display the current working directory, we use the pwd (print
working directory) command.
Listing the Contents of a Directory
To
list the files and directories in the current working directory, we use the ls command.
[me@linux~]$ ls
Desktop Documents Music Pictures Public Templates
Videos
Actually,
we can use the ls command to list the contents of any directory, not
just the current working directory, and there are many other fun things
it can do as well.
Changing the Current Working Directory
To
change our working directory (where we are standing in the tree-shaped maze),
we use the cd command. To do this, type cd followed by the pathname of
the desired working directory. A pathname is the route we take along the
branches of the tree to get to the directory we want. We can specify pathnames
in one of two different ways: as absolute
pathnames or as relative pathnames.
Let’s deal with absolute pathnames first.
Absolute Pathnames
An
absolute pathname begins with the root directory and follows the tree branch
by branch until the path to the desired directory or file is completed. For
example, there is a directory on your system in which most of the system’s programs
are installed. The directory’s pathname is /usr/bin. This means from
the root directory (represented by the leading slash in the pathname) there
is a directory called usr that contains a directory called bin.
[me@linux~]$ cd /usr/bin
[me@linux bin]$ pwd
/usr/bin
[me@linux bin]$ ls
...Listing of many, many files ...
Now
we can see that we have changed the current working directory to /usr/bin and
that it is full of files. Notice how the shell prompt has changed?
As
a convenience, it is usually set up to automatically display the name of the
working directory.
Relative Pathnames
Where
an absolute pathname starts from the root directory and leads to its destination,
a relative pathname starts from the working directory. To do this,
it uses a couple of special notations to represent relative positions in the
file system tree. These special notations are . (dot)
and .. (dot dot).
The
. notation refers to the working directory, and the .. notation refers to
the working directory’s parent directory. Here is how it works. Let’s change the
working directory to /usr/bin again.
Options and Arguments
This
brings us to a very important point about how most commands work. Commands
are often followed by one or more options
that modify their behavior
and, further, by one or more arguments, the items upon which the command
acts. So, most commands look kind of like this:
command -options arguments
Most
commands use options, which consist of a single character preceded by
a dash, for example, -l. Many commands, however, including those
from the GNU Project, also support long
options, consisting of a word preceded
by two dashes. Also, many commands allow multiple short options to
be strung together. In the following example, the ls command is given two
options, which are the l option to produce long format output, and the t option
to sort the result by the file’s modification time.
[me@linux~]$ ls -lt
We’ll add the long option --reverse to reverse the order of the sort.
[me@linux~]$ ls -lt --reverse
Note Command options, like filenames in Linux, are case sensitive. The ls command has a large number of possible options