A Short Introduction to the Command Line

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 previously encountered. This power is available because you have 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, with their many options and possible combinations that 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 GNOME or KDE application menu (depending on which window manager you're using), where you will find a number of emulators under Terminals. Choose the one you want, for example Konsole or XTerm. Depending on your user interface, there may also be an icon that clearly identifies it on the panel (Figure 1.3).

Figure 1.3. The Terminal Icon on the KDE Panel

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). After the prompt is space for you to type your commands. 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
[queen@localhost queen]$ su
Password:
# exit will make you come back to your normal user account
[root@localhost queen]# exit
[queen@localhost queen]$

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

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/queen

Next we will look at a few basic commands, which you should see are quite useful.

cd: Change Directory

The cd command is just like DOS', with a few 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 p 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 usually cannot go 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 let's become 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 by typing exit.

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 that represents 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: this one contains your user ID.

  5. PS1: this variable 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.

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

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

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

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

less: a Pager

The name is a play on words related to the first pager ever used under UNIX, called more. A pager is a program that allows a user to view long files page-by-page (more accurately, screen by screen). The reason that we explain less rather than more is that less is much more intuitive. You should use less to view large files that do not fit on a single screen. For example:

less /etc/termcap

To browse through this file, use the up and down arrow keys. Use q to quit. less can do far more than just that – press h for help to display the various options available.

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 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 very numerous, so we will only explain 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.

  • -s: prints the file size in kilobytes next to each file.

  • -l: prints additional information about the files.

  • -i: prints the inode number (the file's unique number on a file system, see chapter 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.

Here are some examples:

  • ls -R: recursively lists the contents of the current directory

  • ls -is images/ ..: lists the inode number and the size in kilobytes for each file in the images/ directory and in the parent of the current directory.

  • ls -l images/*.png: lists all files in the images/ directory whose names end with .png, including the file .png, if it exists.

Useful Keyboard Shortcuts

There are a number of shortcuts available, with the primary advantage being 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. In addition, the history is persistent from one session to another, so you will not lose 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 is more to editing than just moving one character at a time: Ctrl+A and Ctrl+E, for example, will bring 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.

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.

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 don'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.