4. 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 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 and you will find emulators in the System+Terminals menu. Choose the one you want, for example Konsole or RXvt. 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”).

Figure 1.2. 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). 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.

4.1. cd: Change Directory

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 and pressing the Enter key (or simply by pressing Ctrl-D).

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 the path to your home directory.

  2. PATH: it contains the list of all directories in which the shell should look for executable files 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: 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 entering the man bash command 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.

[Tip]Tip

If one of your environment variables doesn't exist, you can create them temporarily by typing export ENV_VAR_NAME=value. Once this is done, you can verify it has been created:

$ export USERNAME=queen
$ echo $USERNAME
queen

4.3. 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
# This file is edited by fstab-sync - see 'man fstab-sync' for details
/dev/hda2 / ext3 defaults 1 1
/dev/hdc /mnt/cdrom auto umask=0022,user,iocharset=utf8,noauto,ro,exec,users 0 0
none /mnt/floppy supermount dev=/dev/fd0,fs=ext2:vfat,--,umask=0022,iocharset=utf8,sync 0 0
none /proc proc defaults 0 0
/dev/hda3 swap swap defaults 0 0
$ cd /etc
$ cat modprobe.preload
# /etc/modprobe.preload: kernel modules to load at boot time.
#
# This file should contain the names of kernel modules that are
# to be loaded at boot time, one per line.  Comments begin with
# a `#', and everything on the line after them are ignored.
# this file is for module-init-tools (kernel 2.5 and above) ONLY
# for old kernel use /etc/modules

nvidia-agp
$ cat shells
/bin/bash
/bin/csh
/bin/sh
/bin/tcsh

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

4.5. ls: Listing Files

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 sub-directories of directories entered on the command line.

  • -h: if the file size is shown, it is 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 1K 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 4, 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 -ih images/ ..: lists the inode number and the size, in human-readable format, 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.

4.6. Useful Keyboard Shortcuts

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 Mandriva Linux, 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. 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 put 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.