Table of Contents
Abstract
Graphical user interfaces are increasingly becoming important for Linux, but using the mouse is not always the best way to perform daily tasks. The command line provides high flexibility and efficiency. The first part of this chapter provides an introduction to the Bash shell. It is followed by an explanation of the user permission concept in Linux and a list of the most important commands. The chapter closes with a description of the vi text editor.
Text-based applications are especially important for controlling older Linux computers that do not have the resources for demanding display systems. Virtual consoles are used in this case. Six of them are available in text mode. Press Alt-F1 through Alt-F6. The seventh console is reserved for X.
In the KDE taskbar, there is an icon depicting a monitor with a seashell. When you click this icon, 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. Once you have opened the shell, see the prompt on the first line. The prompt usually consists of the username, hostname, and current path, but it can be customized. When the cursor is after this prompt, you can send commands directly to your computer system.
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, 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, 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
--help
. By entering ls
--help
, display all the options for the
ls
command.
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
, enter
ls -l Desktop.
To use the shell efficiently, it is really useful to have some knowledge
of 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 the directories in which
the individual users can store their personal files. Figure 19.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 Filesystem Hierarchy
Standard. The following list provides a brief description of the
standard directories in Linux.
/
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 (such as 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
/sys
“system” file system where all device information for the kernel is gathered
/usr/local
local, distribution-independent extensions
/opt
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:
To repeat a command that has been entered before, press ↑ 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.
Expand a filename 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 filenames 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.
Use the command mkdir (which stands for
make directory)
to create a new
subdirectory called test
by entering mkdir
test.
Now launch the Kate editor by pressing
Alt-F2
and entering 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 example, use an
uppercase T.
View the contents of your home directory again. Instead of typing
ls again, just press ↑
twice and the ls command should reappear at
the prompt. To execute
the command, hit Enter. 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
filename 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 filename and
press Enter 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.
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. You can
start from the current directory.
Address your home directory directly with
~
. This means that 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
. 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 .., 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 expands 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 any 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? lists the files
Testfile1
and Testfile2
. With
ls Test*, the list also includes
Testfile
.
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].
Of 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 Page Up
and Page Down to move forward or backward in the text. To
exit less, 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 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 19.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 like less, use a pipeline.
To view the files in the test
directory, enter the
command ls test | less. The contents of the
test
directory are then 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 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.
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 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. 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 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 options -c
and -f
. For
testing purposes, 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 -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.
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.