Table of Contents
Abstract
Graphical user interfaces are becoming increasingly 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. Text-based
applications are especially important for controlling computers
over slow network links or if you want to perform tasks as root on the command line in an xterm.
The Bash shell is the
default command line interpreter on SUSE Linux.
Linux is a multiuser system and access to files is controlled by user permissions. Whether using the command line or a GUI, it is useful to understand the permission concept. When using the command line, a number of commands are important. The vi text editor is often used when configuring a system from the command line. It is also popular with many system administrators and developers.
In the KDE taskbar, there is an icon depicting a monitor with a seashell. When you click this icon, a terminal window opens in which to enter commands. Konsole, the terminal program, normally runs Bash (Bourne again shell), a program developed as part of the GNU project. On the GNOME desktop, click an icon with a computer monitor in the upper panel to start a terminal that normally runs Bash.
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 shows the contents of the current directory.
Options are prefixed with a hyphen. The command ls
-l, for example, shows the contents of the same
directory in full detail (long listing format). 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 important option that
exists for many commands is --help. By entering ls
--help, display all the options for the
ls command.
It is important to get the “quoting” right. If a filename
contains a space, either escape the space using a back slash
(\) or enclose the filename in single or double
quotes. Otherwise Bash interprets a filename like My
Documents as the names of two files or directories. The
difference between single and double quotes is that variable expansion takes
place within double quotes. Single quotes ensure that the
shell sees the quoted string literally.
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 27.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 (FHS). The following list provides a brief description
of the
standard directories in Linux.
/Root directory, starting point of the directory tree
/homePersonal directories of users
/devDevice files that represent hardware components
/etcImportant files for system configuration
/etc/init.dBoot scripts
/usr/binGenerally accessible programs
/binPrograms needed early in the boot process
/usr/sbinPrograms reserved for the system administrator
/sbinPrograms reserved for the system administrator and needed for booting
/usr/includeHeader files for the C compiler
/usr/include/g++Header files for the C++ compiler
/usr/share/docVarious documentation files
/usr/share/manSystem manual pages (man pages)
/usr/srcSource code of system software
/usr/src/linuxKernel source code
/tmp, /var/tmpTemporary files
/usrAll application programs
/varConfiguration files (such as those linked from
/usr)
/var/logSystem log files
/var/admSystem administration data
/libShared libraries (for dynamically linked programs)
/procProcess file system
/sysSystem file system where all device information for the kernel is gathered
/usr/localLocal, distribution-independent extensions
/optOptional software, larger add-on program packages (such as KDE, GNOME, Netscape)
There are two important features 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.
Complete 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 or GNOME 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 an editor by pressing
Alt-F2
and entering kate Kate in for KDE
gedit for Gedit in GNOME. 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 example 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 . (note the dot at the end). 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 for pathname expansion. There are three 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. As part of set you can
also specify character classes using the syntax
[:class:], where a class
is one of alnum, alpha,
ascii, etc.
Using !
or ^ at the beginning of the group
([!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]
or, using classes, ls Testfile[[:digit:]].
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 27.1.7, “Pipes and Redirection”.
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,
echo "test one" > Content generates a new
file called Content that contains the words
test one. View the file with less
Content.
You can also use a file as the input for a command. For example, with
tr replace characters from standard input that redirected
from the file Content and write the result to standard
output: replace t with x
by calling
tr t x < Content. The output of
tr is sent to the screen.
If you need a new file containing the output, pipe the output of
tr to a file. To test this, change into
test and enter the command tr t x <
../Content > new. Finally, view new
with less new.
Just like the standard output, the standard error output is sent to the
console. 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
>.
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 USB stick 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 is
gzip or, for a even better compression ratio,
bzip2.
Just
enter gzip testarchive.tar (or bzip2
testarchive.tar, but gzip is used in this
example). 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 USB stick.
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 with tar -xvf testarchive.tar.gz
(adding the -z option is no longer required). 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. In Section 27.3, “Important Linux Commands”, find a list of the most important commands and a brief description of their functions.