The command line is the most direct way to send commands to your machine. If you use the GNU/Linux command line, you will soon find that it is much more powerful and capable than other command prompts you may have encountered previously. This power is available because you have access, not only to all X applications, but also to thousands of other utilities in console mode (as opposed to graphical mode) which don't have graphical equivalents, with their many options and possible combinations, which would be hard to access in the form of buttons or menus.
Admittedly most people require a little help to get started. If you're not already working in console mode and are using the graphical interface, the first thing to do is to launch a terminal emulator. Access the main menu of GNOME, KDE or any other window manager you might be using and you will find a number of emulators in the + menu. Choose the one you want, for example or . Depending on your user interface, there may also be an icon which clearly identifies it on the panel (Figure 1.2, “The Terminal Icon on the KDE Panel”).
When you launch this terminal
emulator, you are actually using a shell
. This is the name of the program which you interact
with. You will find yourself in front of the prompt:
[queen@localhost queen]$
This assumes that your user
name is queen and that your machine's name is
localhost
(which is the case if your machine is
not part of an existing network). Following the prompt there is
space for you to type your commands. Note that when you're
root
, the prompt's $
character becomes
a #
(this is true only in the default
configuration, since you may customize all such details in
GNU/Linux). In order to become root
, type
su after launching a
shell
.
[queen@localhost queen]$ su # Enter the root password; (it will not be echoed to the screen) Password: # exit (or Ctrl-D) will take you back to your normal user account [root@localhost queen]# exit [queen@localhost queen]$
When you launch a
shell
for the first time, you normally find yourself in your
home/
directory. To display the name of the directory
you are currently in, type pwd (which stands for Print Working
Directory):
$ pwd /home/queen
Next we will look at a few basic commands which are very useful.
The cd command is just like the DOS
one, with extras. It does just what its acronym states, changes the
working directory. You can use .
and
..
, which respectively stand for the current and
parent directories. Typing cd alone will take you back to
your home directory. Typing cd - will take you back
to the last directory you visited. And lastly, you can specify
peter's home directory by typing cd
~peter (~
on its own means your own
home/
directory). Note that as a normal user, you
cannot usually get into another user's home/
directory (unless they explicitly authorized it or if this is the
default configuration on the system), unless you are root
,
so lets become root
and practice:
$ su Password: # pwd /root # cd /usr/share/doc/HOWTO # pwd /usr/share/doc/HOWTO # cd ../FAQ-Linux # pwd /usr/share/doc/FAQ-Linux # cd ../../../lib # pwd /usr/lib # cd ~peter # pwd /home/peter # cd # pwd /root
Now, go back to being a normal user again by typing exit (or pressing Ctrl-D).
All processes have their environment variables
and the shell
allows you to view them directly with the
echo command. Some interesting variables are:
HOME
: this
environment variable contains a string which represents the
path to your home directory.
PATH
: it contains
the list of all directories in which the shell
should
look for executables when you type a command. Note that unlike
DOS, by default, a shell
will
not look for commands in the current
directory!
PS1
:
determines what your prompt will display, and is often a
combination of special sequences. You may read the bash(1) manual page for more
information by typing man bash in a
terminal.
To have the shell
print a
variable's value, you must put a $
in front of its
name. Here's an example with the echo command:
$ echo Hello Hello $ echo $HOME /home/queen $ echo $USERNAME queen $ echo Hello $USERNAME Hello queen $ cd /usr $ pwd /usr $ cd $HOME $ pwd /home/queen
As you can see, the
shell
substitutes the variable's value before it executes
the command. Otherwise, our cd $HOME example
would not have worked. In fact, the shell first replaced
$HOME
with its value
(/home/queen
) so the line became
cd /home/queen, which is what we
wanted. The same thing happened with the echo
$USERNAME example.
Nothing much to say, this command does just that: it prints the contents of one or more files to the standard output, normally the screen:
$ cat /etc/fstab /dev/hda5 / ext2 defaults 1 1 /dev/hda6 /home ext2 defaults 1 2 /dev/hda7 swap swap defaults 0 0 /dev/hda8 /usr ext2 defaults 1 2 /dev/fd0 /mnt/floppy auto sync,user,noauto,nosuid,nodev 0 0 none /proc proc defaults 0 0 none /dev/pts devpts mode=0620 0 0 /dev/cdrom /mnt/cdrom auto user,noauto,nosuid,exec,nodev,ro 0 0 $ cd /etc $ cat modules.conf shells alias parport_lowlevel parport_pc pre-install plip modprobe parport_pc ; echo 7 > /proc/parport/0/irq #pre-install pcmcia_core /etc/rc.d/init.d/pcmcia start #alias char-major-14 sound alias sound esssolo1 keep /bin/zsh /bin/bash /bin/sh /bin/tcsh /bin/csh /bin/ash /bin/bsh /usr/bin/zsh
The name is a play on words related to the first pager ever used under UNIX® called more. A pager is a program which allows a user to view long files page by page (more accurately, screen by screen). The reason that we discuss less rather than more is that less is more intuitive. You should use less to view large files which will not fit on a single screen. For example:
less /etc/termcap
To browse through this file, use the up and down arrow keys. Press Q to quit. less can do far more than just that: press H for help to display the various options available.
The ls (LiSt) command is equivalent to the dir command in DOS, but it can do much much more. In fact, this is largely because files can do more too. The command syntax for ls is:
ls [options] [file|directory] [file|directory...]
If no file or directory is specified on the command line, ls will list files in the current directory. Its options are numerous, so we will only describe a few of them:
-a
: lists all
files, including hidden files. Remember that
in UNIX®, hidden files are those whose names begin with a
.
; the -A
option lists
“almost” all files, which means every file the
-a
option would print except for “.”
and “..”
-R
: lists
recursively, i.e. all files and subdirectories of directories
entered on the command line.
-h
: prints the file size in a
human readable format next to each file. It means that
you'll see file sizes using suffixes like "K", "M" and "G",
for example "234K" and "132M". Please also note that sizes
are referred in a power of 2, not a power of 10. It means
that 1 K is really 1024 bytes instead of 1000 bytes.
-l
:
prints additional information about the files such as the
permissions associated to it, the owner and owner group, the file's
size and the last-modification time.
-i
:
prints the inode number (the file's unique number in the file
system, see Chapter 9, The Linux File System) next to each file.
-d
: treats
directories on the command line as if they were normal files rather
than listing their contents.
ls -R: recursively lists the contents of the current directory.
ls -ih images/
..: lists the inode number and the size for each
file in the images/
directory as well
as in the parent directory of the current one.
ls -l
images/*.png: lists all files in the
images/
directory whose names end with
.png
, including the file
.png
, if it exists.
There are a number of
shortcuts available, their primary advantage being that they may save you a
lot of typing time. This section assumes you're using the default
shell
provided with Mandrakelinux, bash,
but these keystrokes might work with other shells too.
First: the arrow keys. bash
maintains a history of previous commands which you can view with the up
and down arrow keys. You can scroll up to a maximum number of lines
defined in the HISTSIZE
environment variable. In
addition, the history is persistent from one session to another, so you
won't lose all the commands you typed in previous sessions.
The left and right arrow keys move the cursor left and right on the current line, allowing you to edit your commands. But there's more to editing than just moving one character at a time: Ctrl-A and Ctrl-E, for example, will take you to the beginning and the end of the current line. The Backspace and Del keys work as expected. Backspace and Ctrl-H are equivalent. Del and Ctrl-D can also be used interchangeably. Ctrl-K will delete from the position of the cursor to the end of line, and Ctrl-W will delete the word before the cursor (so will Alt-Backspace).
Typing Ctrl-D on a blank line will let you close the current session, which is much shorter than having to type exit. Ctrl-C will interrupt the currently running command, except if you were in the process of editing your command line, in which case it will cancel the editing and get you back to the prompt. Ctrl-L clears the screen. Ctrl-Z temporarily stops a task, it suspends it. This shortcut is very useful when you forget to type the “&” character after typing a command. For instance:
$ xpdf MyDocument.pdf
Hence you cannot use your shell anymore since the foreground task is allocated to the xpdf process. To put that task in the background and restore your shell, simply type Ctrl-Z and then enter the bg command.
Finally, there are
Ctrl-S and Ctrl-Q, which are used to suspend and restore output to the
screen. They are not used often, but you might type Ctrl-S by mistake (after all, S and
D are close to each other on the keyboard). So, if you
get into the situation where you're typing but you can't see any
characters appearing on the Terminal
, try Ctrl-Q. Note that all the characters you typed between the
unwanted Ctrl-S and Ctrl-Q will be printed to the screen all at once.