G.1 Introduction to Bash

In the KDE taskbar, there is an icon depicting a monitor with a seashell. When you click this icon, a console window opens where you can enter commands. The console normally runs Bash (Bourne again shell), a program developed as part of the GNU project. It is, by far, the most widely used derivative of the Bourne shell (sh). The prompt on the shell's first line usually consists of the username, hostname, and current path, but it can be customized. When the cursor is behind this prompt, you can send commands directly to your computer system.

G.1.1 Commands

A command consists of several elements. The first element is always the actual command, followed by parameters or options. Commands are executed when you press Enter. Before doing so, you can easily edit the command line, add options, or correct typing errors. One of the most frequently used commands is ls, which can be used with or without arguments. Entering the plain ls command in the console shows the contents of the current directory.

Options are prefixed with a hyphen. The command ls -l, for instance, shows the contents of the same directory in full detail. Next to each filename, you can see the date when the file was created, the file size in bytes, and further details, which will be discussed later. One very important option that exists for many commands is the --help option. Entering ls --help displays all the options for the ls command.

You can also use the ls command to view the contents of other directories. To do so, the directory must be specified as a parameter. For example, to see the contents of Desktop, you would enter ls -l Desktop.

G.1.2 Files and Directories

To use the shell efficiently, it is really useful to have some knowledge about the file and directory structures of a Linux system. You can think of directories as electronic folders in which files, programs, and subdirectories are stored. The top level directory in the hierarchy is the root directory, referred to as /. This is the place from which all other directories can be accessed.

The /home directory contains directories where individual users can store their personal files. The following figure shows the standard directory tree in Linux, with the home directories of the example users xyz, linux, and tux.

Figure G-1 Excerpt from a Standard Directory Tree

The directory tree of a Linux system has a functional structure that follows the Filesystem Hierarchy Standard. The following list provides a brief description of the standard directories in Linux.

Table G-1 Standard Linux Directories

Directory

Description

/

Root directory, starting point of the directory tree.

/home

Private directories of users.

/dev

Device files that represent hardware components.

/etc

Important files for system configuration.

/etc/init.d

Boot scripts.

/usr/bin

Generally accessible programs.

/bin

Programs needed early in the boot process.

/usr/sbin

Programs reserved for the system administrator.

/sbin

Programs reserved for the system administrator and needed for booting.

/usr/include

Header files for the C compiler.

/usr/include/g++

Header files for the C++ compiler.

/usr/share/doc

Various documentation files.

/usr/share/man

System manual pages (man pages).

/usr/src

Source code of system software.

/usr/src/linux

Kernel source code.

/tmp, /var/tmp

Temporary files.

/usr

All application programs.

/var

Configuration files (for example, those linked from /usr).

/var/log

System log files.

/var/adm

System administration data.

/lib

Shared libraries (for dynamically linked programs).

/proc

Process file system.

/usr/local

Local, distribution-independent extensions.

/opt

Optional software, larger add-on program packages (such as KDE, GNOME).

G.1.3 Bash Functions

There are two important functions of the shell that can make your work a lot easier:

The history function
To repeat a command that has been entered before, press the Up arrow until the previous command appears at the prompt. Move forward through the list of previously entered commands by pressing the Down arrow. To edit the command line, just move the cursor to the desired position using the arrow keys and start typing. Use Ctrl+R to search in the history.
The expansion function
To expand a filename to its full length after typing its first letters until it can be uniquely identified, type the first letters and then press Tab. If there are several filenames starting with the same letters, obtain a list of them by pressing Tab twice.

Exercise

Now that you know what a command looks like, which directories exist in NovellĀ® Linux Desktop, and how to speed up things when using Bash, put this knowledge into practice with a small exercise.

  1. Open a console from the KDE desktop by clicking the Shell icon.
  2. Enter ls to see the contents of your home directory.
  3. Use the mkdir command (which stands for make directory) to create a new subdirectory called test by entering mkdir test.
  4. Now launch the Kate editor by pressing Alt+F2 and typing kate in the input field. Type a few letters in the editor, then save the file as Testfile in your home directory. Linux distinguishes between uppercase and lowercase; for this exercise, use an uppercase T.
  5. View the contents of your home directory again. Instead of typing ls again, just press the up arrow key twice and the ls command should reappear at the prompt. To execute the command, press Enter. The newly created directory named test should appear in blue letters and Testfile in black. This is how directories and files can be distinguished in a console.
  6. Move Testfile into the subdirectory test with the command mv. To speed this up, use the expansion function: just enter mv T and press Tab. As long as there is no other file beginning with this letter in the directory, the shell expands the filename and adds the string estfile. Otherwise, add a letter or two yourself and press Tab each time to see whether the shell can now expand the name. Finally, type a space then test after the expanded filename and press Enter to execute the command.
  7. At this point, Testfile should no longer be in the directory. Check this by entering ls again.
  8. To see whether the file has been successfully moved, change to the directory test by entering the cd test command. Now enter ls again. You should see Testfile in the listing. Change back to your home directory at any point by entering only cd.
  9. To make a copy of a file, enter cp. For instance, enter cp Testfile Testbackup to copy Testfile to Testbackup. Again, the ls command can be used to see whether both files are in the directory.

G.1.4 Specifying Paths

When working with files or directories, it is important specify the correct path. However, you do not need to enter the entire (absolute) path from the root directory to the respective file. Rather, you can start from the current directory. Address your home directory directly with a tilde (~). Accordingly, there are two ways to list the file Testfile in the directory test: by specifying the relative path with ls test/* or by specifying the absolute path with ls ~/test/*.

To list the contents of home directories of other users, enter ls ~username. In the above-mentioned directory tree, one of the sample users is tux. In this case, ls ~tux would list the contents of the home directory of tux.

Refer to the current directory with a dot (.). The next higher level in the tree is represented by two dots (..). By entering ls .., you can see the contents of the parent directory of the current directory. The command ls ../.. shows the contents of the directory two levels higher in the hierarchy.

Exercise

Try this exercise to practice moving around in the directories of your Novell Linux Desktop system.

  1. Change into your home directory using the cd command. Then create a directory in it with the name test2 by entering mkdir test2.
  2. Change into the new directory using cd test2 and create a subdirectory in it with the name subdirectory. To change to it, use the expansion function: enter cd su then press Tab. The shell will expand the rest of the directory name.
  3. Now try to move the previously created file Testbackup into the current directory (subdirectory) without changing the directory again. To achieve this, enter the relative path to that file: mv ../../test/Testbackup .. The dot at the end of this command is required to tell the shell that the current directory is the destination to move the file to. In this exercise, ../../ refers to your home directory.

G.1.5 Wildcards

Another convenience offered by the shell is four types of wildcards:

Table G-2 Shell Wildcards

Wildcard

Description

?

Matches exactly one arbitrary character.

*

Matches any number of characters.

[set]

Matches one of the characters from the group specified inside the square brackets, which is represented here by the string set.

[!set]

Matches one character other than those identified by set.

Assuming that your test directory contains the files Testfile, Testfile1, Testfile2, and datafile, the command ls Testfile? lists the files Testfile1 and Testfile2. Entering ls Test* will add Testfile. The command ls *fil* shows all the sample files. Finally, you can use the set wildcard to address all sample files whose last character is a number (for example, ls Testfile[1-9]).

Of the four types of wildcards, the most inclusive one is the asterisk (*). It can be used to copy all files contained in one directory to another one or to delete all files with one command. The rm *fil* command, for instance, would delete all files in the current directory whose name includes the string fil.

G.1.6 Less and More

Linux includes two small programs for viewing text files directly in the shell. Rather than starting an editor to read a file like Readme.txt, simply enter the command less Readme.txt to display the text in the console window. Use the Space key to scroll down one page. Use Page Up and Page Down to move forward or backward in the text. To exit the less program, press Q.

Instead of less, you can also use the older program, more. However, it is less convenient because it does not allow you to scroll backwards.

The program less got its name from the precept that less is more and it can also be used to view the output of commands in a convenient way. To see how this works, see the next section, Pipes.

G.1.7 Pipes

Normally, the standard output in the shell is your screen or the console window, and the standard input is the keyboard. To forward the output of a command to an application such as less, use a pipeline (|).

To view the files in the test directory, you would enter the ls test | less command. The contents of the test directory will be displayed with less. This makes sense only if the normal output with ls would be too lengthy. For instance, if you view the contents of the dev directory with ls /dev, you will only see a small portion in the window. But you can view the entire list with ls /dev | less.

It is also possible to save the output of commands to a file. For example, ls test > Content generates a new file called Content that contains a list of the files and directories in test. View the file using the less Content command.

You can also use a file as the input for a command. For example, sort the text lines in Testfile with sort < Testfile. The output of the command sort is sent to the screen. The text is sorted by the first letters of the individual lines.

If you need a new file containing the sorted list, pipe the output of the command sort to a file. To test this, create an unsorted name list in an editor and save it under list in the test directory. Then change to test and enter the sort < unsortedlist > sortedlist command. Finally, view the sorted list with the less command.

Just like the standard output, the standard error output is sent to the console as well. However, to redirect the standard error output to a file named errors, append 2> errors to the corresponding command. Both standard output and standard error are saved to one file named alloutput if you append >& alloutput. Finally, to append the output of a command to an already existing file, the command must be followed by >> instead of a single >.

G.1.8 Archives and Data Compression

Now that you have already created a number of files and directories, consider the subject of archives and data compression. Suppose you want to have the entire test directory packed into one file that you can save on a floppy disk as a backup copy or send by e-mail. To do so, use the tar command (for tape archiver). With tar --help, you can view all the options for the tar command. The most important of these options are explained here:

Table G-3 tar Command Options

Option

Description

-c

(create) Creates a new archive.

-t

(table) Displays the contents of an archive.

-x

(extract) Unpacks the archive.

-v

(verbose) Shows all files on screen while creating the archive.

-f

(file) Selects a filename for the archive file. When creating an archive, this option must always be given as the last one.

To pack the test directory with all its files and subdirectories into an archive named testarchive.tar, use the -c and -f options. For the testing purposes of this example, also add -v to follow the progress of the archiving, although this option is not mandatory. After using cd to change to your home directory where the test directory is located, enter tar -cvf testarchive.tar test. After that, view the contents of the archive file with tar -tf testarchive.tar. The test directory with all its files and directories has remained unchanged on your hard disk. To unpack the archive, you can enter tar -xvf testarchive.tar, but do not try this yet.

For file compression, the obvious choice on Linux is the popular gzip program. Just enter gzip testarchive.tar. With ls, you can now see that the testarchive.tar file is no longer there and that the testarchive.tar.gz file has been created instead. This file is much smaller and, therefore, much better suited for transfer via e-mail or storage on a floppy.

Now, unpack this file in the test2 directory created earlier. To do so, enter cp testarchive.tar.gz test2 to copy the file to that directory. Change to the directory using cd test2. A compressed archive with the .tar.gz extension can be unzipped with the gunzip command. Enter gunzip testarchive.tar.gz, which results in the testarchive.tar file, which then needs to be extracted (or untarred) with tar -xvf testarchive.tar. You can also unzip and extract a compressed archive in one step by adding the -z option. The complete command would be tar -xzvf testarchive.tar.gz. With ls, you can see that a new test directory has been created with the same contents as your test directory in your home directory.

G.1.9 mtools

mtools is a set of commands for working with MS-DOS file systems. The commands included in mtools allow you to address the first floppy drive as a:, just like under MS-DOS, and the commands are like MS-DOS commands except they are prefixed with m.

Table G-4 mtools Commands

Command

Description

mdir a:

Displays the contents of the floppy disk in drive a:.

mcopy Testfile a:

Copies the file Testfile to the floppy disk.

mdel a:Testfile

Deletes Testfile from a:.

mformat a:

Formats the floppy disk in MS-DOS format (using the fdformat command).

mcd a:

Makes a: your current directory.

mmd a:test

Creates the subdirectory test on the floppy disk.

mrd a:test

Deletes the subdirectory test from the floppy disk.

G.1.10 Cleaning Up

After this crash course, you should be familiar with the basics of the Linux shell or command line. You might want to clean up your home directory by deleting the various test files and directories using the rm and rmdir commands. See Important Linux Commands for a list of the most important commands and a brief description of their functions.