In the KDE taskbar, there is an icon depicting a monitor with a seashell. When you click this icon with the mouse, a console window opens in which to 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). Once you have opened the shell, see the prompt on the first line. The prompt usually consists of the user name, host name, and current path, but it can be customized. When the cursor is behind this prompt, you can send commands directly to your computer system: tux $ >.
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 Return. Before doing so, 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 file name, see the date when the file was created, the file size in bytes, and further details, which are covered later. One very important option that exists for many commands is the --help option. By entering ls --help, display all the options for the ls command.
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 where 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 the directories in which the individual users can store their personal files. Figure 24.1: “Excerpt from a Standard Directory Tree” shows the standard directory tree in Linux, with the home directories of the example users xyz, linux, and tux. The directory tree of a Linux system has a functional structure that follows the File System Standard. The following list provides a brief description of the standard directories in Linux.
root directory, starting point of the directory tree.
(private) directories of users.
device files that represent hardware components
important files for system configuration.
generally accessible programs.
programs needed early in the boot process.
programs reserved for the system administrator.
programs reserved for the system administrator and needed for booting.
header files for the C compiler
header files for the C++ compiler.
various documentation files.
system manual pages (man pages).
source code of system software
kernel source code.
all application programs
configuration files (e.g., those linked from /usr)
system log files
system administration data
shared libraries (for dynamically linked programs)
process file system.
local, distribution-independent extensions.
optional software, larger add-on program packages (such as KDE, GNOME, Netscape).
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 UpArrow until the previous command appears at the prompt. Move forward through the list of previously entered commands by pressing ↓. 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 — Expand a file name to its full length after typing its first letters until it can be uniquely identified. To do so, type the first letters then hit Tab. If there are several file names starting with the same letters, obtain a list of them by hitting Tab twice.
Now that you know what a command looks like, which directories exist in SuSE Linux, and how to speed up things when using Bash, put this knowledge into practice with a small exercise.
Open a console from the KDE desktop by clicking the shell icon.
Enter the ls command to see the contents of your home directory.
Now launch the KEdit editor by pressing Alt + F2 and entering kedit 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 example, use an uppercase T.
View the contents of your home directory again. Instead of typing ls again, just press UpArrow twice and the ls command should reappear at the prompt. To execute the command, hit Return. The newly created directory test should appear in blue letters and Testfile in black. This is how directories and files can be distinguished in a console.
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 file name and adds the string estfile. Otherwise, add a letter or two yourself and test Tab each time to see whether the shell can now expand the name. Finally, type a space then test after the expanded file name and press Return to execute the command.
At this point, Testfile should no longer be in the directory. Check this by entering ls again.
To see whether the file has been successfully moved, change into the directory test with the command cd test. Now enter ls again. You should see Testfile in the listing. Change back to your home directory at any point by entering only cd (which stands for change directory).
To make a copy of a file, use cp. For instance, enter cp Testfile Testbackup to copy Testfile to Testbackup. Once again, the command ls can be used to see whether both files are in the directory.
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 ~. Accordingly, there are two ways to list the file Testfile in the directory test: by entering 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. Thus, the command 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 .., 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.
Here is another example to illustrate how to move around in the directories of your SuSE Linux system.
Change into your home directory with the command cd. Then create a directory in it with the name test2 by entering mkdir test2.
Change into the new directory with cd test2 and create a subdirectory in it with the name subdirectory. To change into it, use the expansion function: enter cd su then press Tab. The shell will expand the rest of the directory name.
Now try to move the previously created file Testbackup into the current directory (subdirectory) without changing the directory again. To achieve this, specify 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 which to move the file. ../../, in this example, refers to your home directory.
Another convenience offered by the shell is wild cards. There are four different types of these in Bash:
Matches exactly one arbitrary character
Matches an arbitrary number of characters
Matches one of the characters from the group specified inside the square brackets, which is represented here by the string 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? will list the files Testfile1 and Testfile2. With ls Test*, the list will also include Testfile.
The command ls *fil* shows all the sample files. Finally, you can use the set wild card to address all sample files whose last character is a number: ls Testfile[1-9].
Among the four types of wild cards, the most inclusive one is the asterisk. It could be used to copy all files contained in one directory to another one or to delete all files with one command. The command rm *fil*, for instance, would delete all files in the current directory whose name includes the string fil.
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 less Readme.txt to display the text in the console window. Use Space to scroll down one page. Use PgUp and PgDown to move forward or backward in the text. To exit less, press Q.
The program less got its name from the the precept that less is more and can also be used to view the output of commands in a convenient way. To see how this works, read Section “Pipes”.
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.
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, enter the command ls test | less. The contents of the test directory will be displayed with less. This only makes sense 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. 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 with less Content.
Similarly, you can also use a file as the input for a comand. 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 into test and enter the command sort < unsortedlist > sortedlist. Finally, view the sorted list with less.
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. On the other hand, 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 >.
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 in one file that you can save on a floppy disk as a backup copy or send by e-mail. To do so, use the command tar (for tape archiver). With tar --help, view all the options for the tar command. The most important of these options are explained here:
(for create) Create a new archive.
(for table) Display the contents of an archive.
(for extract) Unpack the archive.
(for verbose) Show all files on screen while creating the archive.
(for file) Choose a file name 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 options -c and -f. 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, 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, now see that the file testarchive.tar is no longer there and that the file testarchive.tar.gz 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 with 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 file testarchive.tar, 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 -xvzf 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.
mtools are 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 an m:
displays the contents of the floppy disk in drive a:
copies the file Testfile to the floppy disk.
deletes Testfile in a:
formats the floppy disk in MS-DOS format (using the fdformat command).
makes a: your current directory
creates the subdirectory test on the floppy disk
deletes the subdirectory test from the floppy disk
After this crash course, you should be familiar with the basics of the Linux shell or command line. You may want to clean up your home directory by deleting the various test files and directories using the rm and rmdir commands. At the end of this chapter, find a list of the most important commands and a brief description of their functions.