2.4. Small introduction to the command line

The command line is the most direct way to send commands to the machine. If you use the GNU/Linux command line, you'll soon find that it is much more powerful and capable than command prompts you may have previously used. The GNU/Linux command line offers direct access to thousands of utilities which don't have graphical equivalents. 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 don't have graphical equivalents, or the many options and possible combinations of which will never be accessible in the form of buttons or menus.

But, admittedly, it requires a little help to get started. That is what this chapter is for. The first thing to do, if you're using KDE, is to launch a terminal emulator. You have an icon which clearly identifies it in the panel (figure 2-3).

Figure 2-3. The terminal icon in the KDE panel

What you have in this terminal emulator when you launch it is actually a shell. This is the name of the program that you interact with. You find yourself in front of the prompt:

[queen@localhost] ~ $

This assumes that your username is queen and that your machine name is localhost (which is usually the case if your machine is not part of an existing network). All that appears after the prompt is what you will have to type. Note that when you are root, the $ of the prompt turns into a #. (This is true only in the default configuration, since you can customize all such details in GNU/Linux). The command to "become" root when you have launched a shell as a user is su:

# Enter the root password; it will not appear on the screen
[queen@localhost] ~ $ su
Password: 
# exit will make you come back to your normal user account
[root@localhost] exit
[queen@localhost] ~ $

Anywhere else in the book, the prompt will be symbolically represented by a $, whether you're a normal user or root. You will be told when you have to be root to execute a command, so remember the su command shown previously. A # at the beginning of a code line will represent 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 the command pwd (which stands for Print Working Directory):

$ pwd
/home/queen

There are a few basic commands which we are now going to look at, and you will soon find that you cannot do without them.

2.4.1. cd: Change Directory

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

$ pwd
/root
$ cd /usr/doc/HOWTO
$ pwd
/usr/doc/HOWTO
$ cd ../FAQ
$ pwd
/usr/doc/FAQ
$ cd ../../lib
$ pwd
/usr/lib
$ cd ~queen
$ pwd
/home/queen
$ cd
$ pwd
/root

Now, become a normal user again.

2.4.2. Some environment variables and the echo command

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 representing your home directory.

  2. PATH This variable 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!

  3. USERNAME This variable contains your login name.

  4. UID Contains your user ID.

  5. PS1 Contains the value for your prompt. It is often a combination of special sequences. You may read the man 1 bash manual page for more information).

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

$ 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 value of the variable before it executes the command. Otherwise, our cd $HOME would not have worked here. In fact, the shell has first replaced $HOME by its value, /home/queen, therefore the line became cd /home/queen, which is what we wanted. It is the same 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 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 conf.modules 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 car-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

It's 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 it's use is much more intuitive. Use less to view large files, which do not fit on a screen. For example:

less /usr/doc/HOWTO/PCMCIA-HOWTO

To navigate the file, use the up and down arrow keys. Use q to quit. However, less can do far more than 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, and this goal is now achieved :-)

2.4.5. ls: listing files (LiSt)

This command is equivalent to dir in DOS, 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 as follows:

ls [options] [file|directory] [file|directory...]

If no file or directory is specified on the command line, ls will print the list of files in the current directory. Its options are very numerous and we will only cite a few of them:

  1. -a List all files, including hidden files (in Unix hidden files are files whose names begin with .); the option -A lists "almost" all files, which means every file the -a option would print except "." and "..";

  2. -R List recursively, i.e. all files and subdirectories of the directories mentioned in the command line;

  3. -s Displays the file size in kilobytes next to each file;

  4. -l Displays additional information about the files;

  5. -i Displays the inode number (the file's unique number on a filesystem, see chapter The GNU/Linux filesystem: ext2fs) next to each file;

  6. -d Displays the directories as normal files instead of listing their contents.

Some examples:

  1. ls -R lists the contents of the current directory recursively;

  2. ls -is images/ .. lists the files in directory images/ and in the parent directory, and prints for each file its inode number and size in kilobytes;

  3. ls -al images/*.gif lists all files (including any hidden files) in directory images/ with names ending in .gif. Note that this also includes the file .gif if one exists.

2.4.6. Useful keyboard shortcuts

Many keystrokes are available which can save much typing and this section will present some of the most useful ones. This section assumes you are using the default shell provided with Linux-Mandrake, Bash, but these keystrokes should work with other shells too. In this section, C-<x> means Ctrl+<x> (hold down Ctrl key, press key <x>, release both keys).

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 have typed in a previous session.

The left and right arrow keys move the cursor left and right in the current line, so you can edit your commands this way. But there is more to editing: C-a and C-e, for example, will bring you to the beginning and to the end of the current line respectively. The Backspace and Del keys work as expected. An equivalent to Backspace is C-h and an equivalent to Del is C-d. C-k will delete all the line from the position of the cursor to the end of line, and C-w will delete the word before the cursor.

Typing C-d on a blank line will make you close the current session, which is much shorter than having to type exit. C-c will interrupt the currently running command, except if you were in the process of editing, in which case it will cancel the editing and get you back to the prompt. C-l clears the screen.

Finally, there is the case of C-s and C-q: these keystrokes respectively suspend and restore the flow of characters on a terminal. They are very seldom used, but it may happen however that you type C-s by mistake. So, if you strike keys but you don't see any character appearing on the terminal, try C-q first and beware: all characters you have typed between the unwanted C-s and C-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. 2000.
http://www.linux-mandrake.com/