2.4. Small Introduction to the Command Line

The command line is the most direct way to send commands to a machine. If you use the GNU/Linux command line, you will soon find that it is much more powerful and capable than command prompts you may have previously used. The reason for it is that you have direct access, not only to all X applications, but also to thousands of utilities in console mode (as opposed to graphical mode) which do not have graphical equivalents, or the many options and possible combinations of which will never (or not yet!) be accessible in the form of buttons or menus.

But, admittedly, it requires a little help to get started. The first thing to do is to launch a terminal emulator. While accessing either your GNOME or KDE application menu, you will find them in Terminal. Then, choose the one you want, for example Terminal or xterm. You also have an icon which clearly identifies it on the panel (figure 2-3).

Figure 2-3. The Terminal Icon on the KDE Panel

When you launch a this terminal emulator, you actually use a shell. This is the name of the program with which you interact. You find yourself in front of the prompt:
[pingusa@localhost] ~ $
This assumes that your user name is pingusa and that your machine's name is localhost (this is the case if your machine is not part of an existing network). Typically, after the prompt is space for you to type whatever you need to type. Note that when you are root, the prompt's $ character turns into a # (this is true only in the default configuration, since you can customize all such details in GNU/Linux). In order to become root, type su after launching a shell.
# Enter the root password; it will not appear on the screen
[pingusa@localhost] ~ $ su
Password: 
# exit will make you come back to your normal user account
[root@localhost] pingusa # exit
[pingusa@localhost] ~ $
Anywhere else in the book, the prompt will be symbolically represented by a $, whether you are a normal user or root. You will be told when you have to be root to execute a command, so please remember the su. However, when the # character is placed at the beginning of a line of code, it represents a comment.

When you launch a shell for the first time, you normally find yourself in your home directory. To display the directory you are currently in, type pwd (which stands for Print Working Directory):

$ pwd
/home/pingusa

Now, we will look at a few basic commands, and you will soon find that you cannot live without them!

2.4.1. cd: Change Directory

The cd command is just like DOS', with a few extras. It does just what its acronym states, change the working directory. You can use . and .., which stand respectively for the current and parent directories. Typing cd alone will bring you back to your home directory. Typing cd - will bring you back to the last directory you visited. And lastly, you can specify the peter's home directory by typing cd ~peter (~ alone means your own home directory). Note that as a normal user, you usually cannot go into another user's home directory (unless she explicitly authorized it or if this is the default configuration on the system), except if you are root, so let's be root and practice:

$ 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.

2.4.2. Some Environment Variables and the echo Command

All processes have their environment variables and the shell allows you to view them directly with the echo command. Some interesting variables are:

  1. HOME: this environment variable contains a string which represents your home directory.

  2. PATH: this one holds 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!

  3. USERNAME: this variable contains your login name.

  4. UID: this one holds your user ID.

  5. PS1: this variable contains your prompt's definition. It is often a combination of special sequences. You may read the bash(1) manual page for more information.

To have the shell print a variable's value, you must put a $ in front of its name. Here, the echo command will help you:

$ echo Hello
Hello
$ echo $HOME
/home/pingusa
$ echo $USERNAME
pingusa
$ echo Hello $USERNAME
Hello pingusa
$ cd /usr
$ pwd
/usr
$ cd $HOME
$ pwd
/home/pingusa

As you can see, the shell substitutes the variable's value before it executes the command. Otherwise, our cd $HOME would not have worked here. In fact, the shell first replaced $HOME by its value, /home/pingusa. Therefore, the line became cd /home/pingusa, which is what we wanted. It is the same thing for echo $USERNAME and so on.

2.4.3. cat: Print the Contents of One or More Files to the Screen

Nothing much to say, this command does just that: print 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

2.4.4. less: a Pager

Its name is a play on words related to the first pager ever under Unix, which was called more. A pager is a program which allows a user to view long files page by page (more accurately, screen by screen). We speak about less rather than more because its use is much more intuitive. You should use less to view large files, which do not fit on a screen. For example:

less /etc/termcap

To browse through this file, use the up and down arrow keys. Use q to quit. However, less can do far more than just that. In fact, just type h for help, and take a look. But anyway, the goal of this section is just to enable you to read long files, so we already reached our goal :-)

2.4.5. ls: Listing Files

The ls (LiSt) command is equivalent to DOS' dir command, but it can do much much more. In fact, this is largely due to the fact that 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 very numerous and we will only explain a few of them:

Some examples:

2.4.6. Useful Keyboard Shortcuts

Many keystrokes are available and their main advantage is that they save you a lot of typing time. This section assumes you are using the default shell provided with Mandrake Linux, bash, but these keystrokes should 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. Moreover, the history is persistent from one session to another, so you will not lose the commands you typed in previous session.

The left and right arrow keys move the cursor left and right on the current line, so you can edit your commands this way. But there is more to editing: Ctrl+A and Ctrl+E, for example, will bring you respectively to the beginning and the end of the current line. The Backspace and Del keys work as expected. An equivalent to Backspace is Ctrl+H and an equivalent to Del is Ctrl+D. Ctrl+K will delete all the line from the position of the cursor to the end of line, and Ctrl+W will delete the word before the cursor.

Typing Ctrl+D on a blank line will make 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.

Finally, there is the case of Ctrl+S and Ctrl+Q: these keystrokes respectively suspend and restore the flow of characters on a terminal. They are very seldom used, but it may happen that you type Ctrl+S by mistake (after all, S and D are very close to each other on a keyboard...). So, if you strike keys but do not see any character appearing on the Terminal, try Ctrl+Q first and beware: all characters you typed between the unwanted Ctrl+S and Ctrl+Q will be printed to the screen all at once.


Tux on Star from MandrakeSoft Linux is a registered trademark of Linus Torvalds. All other trademarks and copyrights are the property of their respective owners.
Unless otherwise stated, all the content of these pages and all images are Copyright MandrakeSoft S.A. and MandrakeSoft Inc. 2001.
http://www.mandrakelinux.com/