Chapter 20. Working with the Shell

Contents

20.1. Using the Bash Shell
20.2. Users and Access Permissions
20.3. Important Linux Commands
20.4. The vi Editor

Abstract

Although graphical user interfaces have become very important and user-friendly, using them is not the only way to communicate with your system. A command line interpreter, in Unix/Linux called the shell, provides a highly flexible and efficient means for text-oriented communication with your system.

In administration, shell-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.

This chapter deals with a couple of basics you need to know for making efficient use of the command line: the directory structure of Linux, the user and permission concept of Linux, an overview of important shell commands, and a short introduction to the vi editor, which is a default editor always available in Unix and Linux systems.

20.1. Using the Bash Shell

For UNIX or Linux several shells are available which differ slightly in behavior and in the commands they accept. The default shell in openSUSE™ is Bash (GNU Bourne-Again Shell).

If you are logged in to a graphical user interface, you can start a (login) shell parallel to the user interface or in a terminal window within the graphical user interface. Press Ctrl-Alt-F2 to leave the graphical user interface and access a login shell. After login, the prompt shows your login name followed by @ and the hostname of your computer. The hostname is followed by a colon and the path to the current directory. If you have logged in as system administrator, root, Bash indicates this with a hash symbol, #. Directly after login, the current directory is usually the home directory of the user account with which you have logged in, indicated by the tilde symbol, ~. When you are logged in on a remote computer the information provided by the prompt always shows you which system you are currently working on. You can now enter commands and execute tasks. To log out from the shell, enter exit and press Alt-F7 to switch back to the graphical user interface. You will find your desktop and the applications running on it unchanged.

To start a terminal window within the graphical user interface in KDE or GNOME press Alt-F2 and enter xterm (or click the Konsole or GNOME terminal icon in the panel). This opens a terminal window on your desktop. As you are already logged in to your desktop the prompt shows the usual login and path information. You can now enter commands and execute tasks just like in any shell which runs parallel to your desktop. To close the terminal window press Alt-F4 .

The Konsole or the GNOME Terminal window appears, displaying the prompt at the first line, see Figure 20.1, “Example of a Bash Terminal Window”. The prompt usually shows your login name (in this example, tux), the hostname of your computer (here, knox), and the current path (in this case, your home directory, indicated by the tilde symbol, ~). When you are logged in on a remote computer this information always shows you which system you are currently working on. When the cursor placed behind this prompt, you can send commands directly to your computer system.

Figure 20.1. Example of a Bash Terminal Window

Example of a Bash Terminal Window

Because the shell does not offer a graphical overview of directories and files like the tree view in a file manager, it is useful to have some basic knowledge of the default directory structure in Linux.

20.1.1. The Directory Structure

The following table provides a short overview of the most important higher-level directories you find on a Linux system. Find more detailed information about the directories and important subdirectories in the following list.

Table 20.1. Overview of a Standard Directory Tree

Directory

Contents

/

Root directory—the starting point of the directory tree.

/bin

Essential binary files, such as commands that are needed by both the system administrator and normal users. Usually also contains the shells, such as Bash.

/boot

Static files of the boot loader.

/dev

Files needed to access host-specific devices.

/etc

Host-specific system configuration files.

/lib

Essential shared libraries and kernel modules.

/media

Mount points for removable media.

/mnt

Mount point for temporarily mounting a file system.

/opt

Add-on application software packages.

/root

Home directory for the superuser root.

/sbin

Essential system binaries.

/srv

Data for services provided by the system.

/tmp

Temporary files.

/usr

Secondary hierarchy with read-only data.

/var

Variable data such as log files

/windows

Only available if you have both Microsoft Windows* and Linux installed on your system. Contains the Windows data.

The following list provides more detailed information and gives some examples which files and subdirectories can be found in the directories:

/bin

Contains the basic shell commands that may be used both by root and by other users. These commands include ls, mkdir, cp, mv, rm, and rmdir. /bin also contains Bash, the default shell in openSUSE.

/boot

Contains data required for booting, such as the boot loader, the kernel, and other data that is used before the kernel begins executing user mode programs.

/dev

Holds device files that represent hardware components.

/etc

Contains local configuration files that control the operation of programs like the X Window System. The /etc/init.d subdirectory contains scripts that are executed during the boot process.

/home/username

Holds the private data of every user who has an account on the system. The files located here can only be modified by their owner or by the system administrator. By default, your e-mail directory and personal desktop configuration are located here.

[Note]Home Directory in a Network Environment

If you are working in a network environment, your home directory may be mapped to a directory in the file system other than /home.

/lib

Contains essential shared libraries needed to boot the system and to run the commands in the root file system. The Windows equivalent for shared libraries are DLL files.

/media

Contains mount points for removable media, such as CD-ROMs, USB sticks, and digital cameras (if they use USB). /media generally holds any type of drive except the hard drive of your system. As soon as your removable medium has been inserted or connected to the system and has been mounted, you can access it from here.

/mnt

This directory provides a mount point for a temporarily mounted file system. root may mount file systems here.

/opt

Reserved for the installation of additional software. Optional software and larger add-on program packages, such as the KDE and GNOME desktop environments, can be found here.

/root

Home directory for the root user. Personal data of root is located here.

/sbin

As the s indicates, this directory holds utilities for the superuser. /sbin contains binaries essential for booting, restoring, and recovering the system in addition to the binaries in /bin.

/srv

Holds data for services provided by the system, such as FTP and HTTP.

/tmp

This directory is used by programs that require temporary storage of files. By default, the data stored in /tmp is deleted regularly.

[Note]Storing Files in /tmp

Do not store any files in /tmp that you want to keep. This directory is automatically cleaned up by the system and files are removed in the process.

/usr

/usr has nothing to do with users, but is the acronym for UNIX system resources. The data in /usr is static, read-only data that can be shared among various hosts compliant to the Filesystem Hierarchy Standard (FHS). This directory contains all application programs and establishes a secondary hierarchy in the file system. /usr holds a number of subdirectories, such as /usr/bin, /usr/sbin, /usr/local, and /usr/share/doc.

/usr/bin

Contains generally accessible programs.

/usr/sbin

Contains programs reserved for the system administrator, such as repair functions.

/usr/local

In this directory, the system administrator can install local, distribution-independent extensions.

/usr/share/doc

Holds various documentation files and the release notes for your system. In the manual subdirectory, find an online version of this manual. If more than one language is installed, this directory may contain versions of the manuals for different languages.

Under packages, find the documentation included in the software packages installed on your system. For every package, a subdirectory /usr/share/doc/packages/packagename is created that often holds README files for the package and sometimes examples, configuration files, or additional scripts.

If HOWTOs are installed on your system /usr/share/doc also holds the howto subdirectory in which to find additional documentation on many tasks relating to the setup and operation of Linux software.

/var

Whereas /usr holds static, read-only data, /var is for data which is written during system operation and thus is variable data, such as log files or spooling data. For example, the log files of your system are in /var/log/messages (only accessible for root).

/windows

Only available if you have both Microsoft Windows and Linux installed on your system. Contains the Windows data available on the Windows partition of your system. Whether you can edit the data in this directory depends on the file system your Windows partition uses. If it is FAT32, you can open and edit the files in this directory. For an NTFS file system, however, you can only read your Windows files from Linux, but not modify them. Learn more in Section “Accessing Files on Different Operating Systems on the same Computer” (Chapter 11, Copying and Sharing Files, ↑Start-Up).

20.1.2. Useful Bash Features

Entering commands in Bash might involve a lot of typing. In the following, get to know some features of the Bash that can make your work a lot easier and save you a lot of typing.

20.1.2.1. History and Completion

By default, Bash “remembers” commands you have entered. This feature is called history. To repeat a command that has been entered before, press until the desired command appears at the prompt. Press to move forward through the list of previously entered commands. Use Ctrl-R to search in the history.

You can edit the selected command, for example, changing the name of a file, before you execute the command by pressing Enter. To edit the command line, just move the cursor to the desired position using the arrow keys and start typing.

Completing a filename or directory name to its full length after typing its first letters is another helpful feature of Bash. To do so, type the first letters then press →|. If the filename or path can be uniquely identified, it is completed at once and the cursor moves to the end of the filename. You can then enter the next option of the command, if necessary. If the filename or path cannot be uniquely identified (because there are several filenames starting with the same letters), the filename or path is only completed up to the point where it is again getting ambiguous. You can then obtain a list of the options available by pressing →| a second time. After this, you can enter the next letters of the file or path then try completion again by pressing →|. When completing filenames and paths with →|, you can simultaneously check whether the file or path you want to enter really exists (and you can be sure of getting the spelling right).

20.1.2.2. Wild Cards

Another convenience offered by the shell is wild cards for pathname expansion. Wild cards are characters that can stand for other characters. There are three different types of these in Bash:

?

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

  • The command ls Testfile? lists the files Testfile1 and Testfile2.

  • With ls Test*, the list also includes Testfile.

  • The command ls *fil* shows all the sample files.

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

20.1.2.3. Viewing Files with Less and More

Linux includes two small programs for viewing text files directly in the shell: less and more. 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 20.1.2.4, “Redirection and Pipes”.

20.1.2.4. Redirection and Pipes

Normally, the standard output of the shell is your screen or the console window and the standard input is the keyboard. However, the shell provides functions by which you can redirect the input or the output to another object, such as a file or another command. With the help of the symbols > and <, for example, you can forward the output of a command to a file (output redirection) or use a file as input for a command (input redirection). For example, if you want to write the output of a command such as ls to a file, enter ls -l > file.txt. This creates a file named file.txt that contains the list of contents of your current directory as generated by the ls command. However, if a file named file.txt already exists, this command overwrites the existing file. To prevent this, use >>. Entering ls -l >> file.txt simply appends the output of the ls command to an already existing file named file.txt. If the file does not exist, it is created.

Sometimes it is also useful to use a file as the input for a command. For example, with the tr command, you can replace characters redirected from a file and write the result to the standard output, your screen. Suppose you want to replace all characters t of your file.txt from the example above with x and print this to your screen. Do so by entering tr t x < file.txt.

Just like the standard output, the standard error output is sent to the console. 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.

Using pipelines or pipes is also a sort redirection, although the use of the pipe is not constrained to files. With a pipe (|), you can combine several commands, using the output of one command as input for the next command. For example, to view the contents or your current directory in less, enter ls | 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.