Chapter 19 First Steps with Linux Since UNIX is such a complex system, we can only cover the most important aspects here. This book is not aimed at replacing the existing literature on Linux (or UNIX)-this would be an impossible task. We recommend that former DOS users consider buying a few good books You can find examples in the literature recommendations in the appendix. If you already have some non-Linux, Unix literature, this may be sufficient, as a large proportion of information is relevant beyond system borders. Some books, or extracts from them, which are part of the GPL, can be found as .dvi or PostScript files in package doc, series books. They can be viewed in X11 with xdvi or gv (Ghostview) and printed out in full, or by page, with lpr. Until you have obtained these books, this chapter should give you a short overview and, as well, help you while "experimenting" with Linux for the first time. Being a UNIX novice, once you have successfully installed the system, you should log in with your `user name' ­ the one you specified during the installation ­ (not as root!), firstly, because there are a lot of settings that have already been dealt with for you and secondly, you will then only be responsible for your private home directory. This is also for security reasons ­ to avoid deleting or changing system-relevant data. There is no undelete in Linux (yet) as there is in DOS. So, if you delete system files by accident, you will probably have to reinstall the entire system. The first steps in Linux are somewhat complicated, since you are the system administrator as well, not an easy task for a novice user. 19.1 Logging in, the User "root", Adding Users The multi-user capabilities of Linux mean that whenever you want to use the system, you have to log in on the console (see Section 19.4 page 430) each time you want to use the system. 1 This procedure, an essential part of the security system of a modern multi-user system, is called logging in; 1 If you have configured a graphical login (cf. Section 3.6.6 page 98), then this is also valid for the procedure described below! 427 19. First Steps with Linux this ensures that each user is allocated his own working environment and can only access his own files. You enter your user name (e. g., `newbie') and your password (e. g. xxxxxx); you must of course replace the characters xxxxxx with the password you chose yourself! The characters xxxxxx are not displayed on the monitor; just type in the characters and then press 2: login: newbie If you have successfully "logged in", you will find yourself in the home direc- tory of the corresponding user (e. g., /home/newbie for the user `new- bie'). If you want to leave the console, you can log out using the command logout. The user `root' is the system administrator and is allowed to do every- thing. All critical system information files can only be changed by `root'. Therefore you should only log in as `root' if you plan to configure some- thing or execute system specific tasks. Never log in as `root' if it is not necessary (most beginners forget this!) Then you will protect your system against inadvertent changes, which in certain cases might mean having to re-install the whole system. Some things that only `root' can do: * mounting filesystems (such as CD's, floppies, installing software). This right can be given to selected users by adding the option user to the corresponding device in /etc/fstab(cf. Section 19.11.2 page 444). * adding and deleting users (cf. Section 3.6.7 page 99) * installing a new kernel (cf. Chapter 13 page 317). * configuring the system * shutting down the system (cf. Section 19.2). * starting yast (cf. Chapter 3 page 71). 19.2 Shutting Down and Booting the System IMPORTANT! You should never turn off the machine while it is running, nor press the reset button ( reset)! If you switch it off without shutting it down properly, you risk loss of data and damage to your filesystem. The commands for shutting down your system properly are shown in Ta- ble 19.1 on the facing page. The shutdown command can only be invoked by `root'. To shut down your system, log in as `root' and enter the commands shutdown -h now or shutdown -r now. Alternatively, you can use Ctrl + Alt + Del ,which you might already know from booting DOS. This combination does not work if you are currently running X11. Nevertheless, this method can be used by any user on any virtual console. 2 In case you haven't yet set a password, just press 428 19.3. Commands ­ Entries on the Command Line shutdown -h now halts the system; when you see the out- put: "the system is halted" you can switch off your machine shutdown -r now reboots the system immediately Table 19.1: Commands for shutting down your Linux system 19.3 Commands ­ Entries on the Command Line Even though the Linux world is becoming more and more colorful and user- friendly ­ menu-driven programs, for example ­ there will still be situations ­ in an emergency, or when a menu-driven program for a specific task is not available ­ where you need to enter a UNIX-"command". What is a UNIX Command? The following are all UNIX commands: * executable programs * Shell scripts * Scripts using scripting languages (Perl, Tcl, etc). * Shell aliases (such as shell macros). They have one thing in common: they are all files. If you launch such a command in Linux, you tell the shell to find the file with that name (you need the search path, which is set in PATH, to find the file, and when it has been found, to run it, if it has the correct user permissions (executable). But what happens if the program (for example, the copy command) needs additional parameters or file names? This is fairly straightforward. Parameters are additional arguments for a command. They might tell the command optional items, such as file names, etc. The parameters directly follow the command, separated by at least one blank3. In addition it can be useful to control the behavior of the command, (if you want a long list of file names instead of a short one, for example). This is achieved by means of "options". Options always follow the actual command, before any parameters. There are a few rare exceptions where commands have been "messily" programmed. In general, options are preceded by a dash (e. g. -la) and follow one of two patterns (see Table 19.2 on the next page): If you want to set multiple options you can "cumulate", or add them all together behind one dash (lots of Linux programs understand this rule, but not all of them). For example: 3 This also implies that a blank can never be part of the command itself, as it serves as a separator between parameters; you may add blanks for a command if you enclose them in quotation marks ("" or '') 429 19. First Steps with Linux -a short version, UNIX typical --all long version, the so-called GNU notation Table 19.2: Command options -a -f -r -u or -afru or -frua This example also shows that the order is irrelevant. Well, even here there are exceptions to the rule. To slightly complicate the issue, some options themselves understand op- tional parameters. For example: -f or -f In some (very) rare cases, there has to be a blank between the option and its parameter. Normally you should use a blank. Examples To summarize, this is what commands look like in Linux: (Examples)4 earth: # fdisk earth: # lsmod earth: # ls With options, they look like this: earth: # fdisk -l earth: # ls -l -a earth: # ls -la With parameters: earth: # fdisk /dev/hda earth: # ls /tmp And using both options and parameters: earth: # ls -la /tmp earth: # rpm -qpl .rpm earth: # gcc -o It is essential that the blank separates the different parts of the command from each other, which is why it is a special character for Linux commands. 19.4 Virtual Consoles Linux is a multi-user and multi-tasking system. You will learn to appreciate the advantages of these features, even on a standalone PC system. 4 Remember: "earth: # " shows the prompt, you don't have to enter earth: and `#' yourself. 430 19.5. Directories and Filenames In text mode, there are six virtual consoles available. You can switch be- tween them by using Alt + F1 to Alt + F6 . The seventh console is re- served for X11.5 If you want to switch to a console from X11 without leaving X11, you should use Ctrl + Alt + F1 to Ctrl + Alt + F6 . Alt + F7 brings you back to X11. 19.5 Directories and Filenames All information ­ whether it is text, images, database files or information on system configuration ­ is stored in "files", which in turn are stored in pre- determined "directories" (cf. Appendix D page 469). With various tools and programs you can access these files in different directories, in order to look at the contents of these files or change them where necessary. The character for separating directories (`\' in DOS) is a `/' on UNIX. Thus, a path is a string in which all directories are separated by `/'. A single `/' represents the topmost directory, the "root directory". UNIX distinguishes between upper- and lower case names, i.e. Emil refers to a different filename than emil. Separating a filename into its name and extension is usually not necessary, but there are some programs that require a specific extension (such as .dvi in LATEX). A nice little feature when entering file or directory names is the function of the Tab (tabulator key). Just enter the first few letters of the desired file name and press Tab .The Shell will now complete the file name for you (insofar as it is clearly distinguishable by these letters). Pressing the tab key twice will display all available possibilities. This feature is called "globbing" in Unix. 19.6 Working with Directories After logging in (cf. Section 19.1 page 427), you will be in your home direc- tory. The name of the current directory can be checked by typing pwd (print working directory): newbie@earth:/home/newbie > pwd /home/newbie To change to another directory, use the cd command (the same as in DOS). Typing: newbie@earth:/home/newbie > cd /usr/bin newbie@earth:/usr/bin > changes to the directory /usr/bin. newbie@earth:/home/newbie > cd latex newbie@earth:/home/newbie/latex > changes to latex, provided the directory /home/newbie/latex exists. If you invoke cd without any argument, you will be brought back to your home directory. This can be reached by typing a tilde (` ') as well. 5 You can assign more consoles via /etc/inittab. 431 19. First Steps with Linux So typing: newbie@earth:/home/newbie > cd /latex changes to the directory latex in your home directory. As with DOS, `.' signifies the current directory, whereas `..' stands for the parent directory. You can create new directories with mkdir (make directory). The command: newbie@earth:/home/newbie > mkdir texts creates the directory text directly underneath the current directory. Empty directories can be removed using the command rmdir (remove directory). 19.7 Working with Files Until they are (perhaps) one day replaced by objects or symbols, files will continue to play a central role while working with the computer. Therefore a huge variety of file-related commands exist in Linux. 19.7.1 Information on Files The command ls (list) outputs the contents of your current directory. A list of all filenames and directories is shown. Directory names in the list are marked with a `/'. By adding a parameter, the contents of a different directory can be displayed: newbie@earth: > ls /usr/bin Extensions are not required in order to run programs, as is the case in DOS (.exe or .com). Instead, execution of programs is controlled by one of three permissions which may be set for each file by its owner. See Section 19.8 page 437 for more information on file permissions. A useful option to ls is -l. This gives you a more detailed list of filenames, including the permissions, owner, group and size of the file: newbie@earth: > ls -l This will create an output such as screen output 19.7.1. drwxr-xr-x 6 newbie users 1024 Mar 21 12:39 ./ drwxr-xr-x 4 newbie users 1024 Mar 21 17:13 ../ drwxr-xr-x 2 newbie users 1024 Nov 6 16:19 bin/ -rwxr-xr-x 1 newbie users 4160 Mar 21 12:38 check* drwxr-xr-x 2 newbie users 1024 Nov 6 16:23 etc/ drwxr-xr-x 2 newbie users 1024 Nov 6 16:19 sbin/ drwxr-xr-x 12 newbie users 1024 Nov 6 18:20 usr/ -rw-r--r-- 1 newbie users 185050 Mar 15 12:33 xvi.tgz -rw-r--r-- 1 newbie users 98444 Mar 14 12:30 xvnews.tgz Screen output 19.7.1: Output of ls -l The meaning of the entries in screen output 19.7.1 is explained in Table 19.3 on the next page. 432 19.7. Working with Files Permissions The first character indicates the file type. `d' stands for directory, `l' for link and `-' for a normal file. The next nine characters indicate permissions for the user, the group and all other users (in groups of three). `r' stands for read, `w' for write, and `x' for ex- ecutable. For example, `-rw-r--r--' refers to a file which can be read by the owner, the group and all others, but it can only be changed by the owner. See the manpage for chmod (man chmod). Owner The owner of the file. See the manpage for chown (man chown). Group The group the file belongs to. See the manpage for chgrp (man chgrp). Size The file's size in bytes. Last change Date when the file was last changed. Files that were changed more than a year ago are marked with the year instead of hours:minutes. Name The file or directory name. Table 19.3: Explanation of UNIX file attributes 19.7.2 Wildcards Compared to DOS, the wildcard interpreting options of the shell (e. g., bash) are much more powerful. For example: ls *a???.? returns all files in the current directory which have an `a' as the sixth letter from the end and a `.' as the last but one character. Instead of a single character, you can give a whole range of different characters. For example, the letters `a', `b', `c', `d', `e', `f'. This would be written like this: ls *[a-f]???.? You can even search in non-alphabetical order: ls *[1,3-5,M-P,a,k]???.? 433 19. First Steps with Linux 19.7.3 Contents of Files You can view the contents of a file with less and more, a page at a time. With more, you can scroll forwards with (space) and backwards with b (see Table 19.4). However, less is considerably more comfortable, which is why it is generally preferred. With newbie@earth: > less /etc/login.defs the file /etc/login.defs is loaded into the "viewer" less; the first page of the file is displayed, together with an inverse status line on the bottom edge of the screen. With space bar you can scroll forwards a page at a time, with b backwards; see also the list in Table 19.4. h Help q Quit e One line forwards y One line backwards f One page forwards b One page backwards d Half a page forwards u Half a page backwards g Go to beginning of file G Go to end of file / Start search n Jump to next marker v Load file into the editor F View a "growing" (log )file c Switch from F mode to "normal" Table 19.4: Keys used for the "viewer" less (Selection) With the / key you can enter a search item (e. g. yes). starts your search. n jumps to the next occurrence of the item. Of special interest is the key F . It allows you to view a log file, for example, as entries are added to it. With Ctrl + c you can leave this mode. With the h key, all the features of less are displayed. q leaves the help mode. Press q again to quit the program. To modify files you should start an editor; The standard UNIX editor is vi, and it is well worth spending some time becoming familiar with some of its basic commands. 19.7.4 Hidden Files Hidden files are a special kind of file. The filenames for these files begin with a dot and are only seen if you use ls with the option -a. In your home 434 19.7. Working with Files directory, just enter ls -a. Now, you should see all files, even the hidden ones (like .profile or .xinitrc). Hidden files are protected from any unintentional deletion using rm * (see Section 19.7.5). These files have to be deleted separately, using rm <.filename>. If you enter rm .*, all hidden files from the current directory will be deleted! If the option -r is added (recursive; rm -r .*), all files of the parent directory are deleted as well (they are named `../bla', which is covered by `.*' as well)! So be extremely careful with -r! 19.7.5 Copying, Renaming and Deleting Files The command for copying files in Linux is cp: newbie@earth: > cp source target To copy the file /etc/XF86Config into your home directory, you would use: newbie@earth: > cp /etc/XF86Config Files can be removed using the rm command. A very handy option is -r which deletes recursively (removing all subdirectories and their files as well; this is comparable to deltree in DOS). Entering: newbie@earth: > rm -r bin deletes the directory bin and all files and subdirectories within it. Please use this option with extreme caution, since there is absolutely no means of recovering (restoring) deleted files! You can move files by typing mv. The syntax is identical to cp: newbie@earth: > mv xvnews.tgz XVNEWS.tgz This moves the file xvnews.tgz to XVNEWS.tgz, which is just the same as renaming it. It gets more interesting when you move whole directory trees: newbie@earth: > mv bin /latex This moves the directory bin (if there is one) to /latex. All the files that were in bin will now be found in /latex. Even this command should be used with care, since it is very easy to move whole directory trees to places that you do not remember afterwards. Moving a complete directory tree is only possible within the same filesystem (or partition). 19.7.6 Searching for Files and for Strings Within Files This leads to another useful command: find. To search all subdirectories of the current directory for the file emil, you should enter: newbie@earth: > find . -name "emil" The first argument gives the directory where the search should commence. The option name expects a search string (wildcards are allowed). Thus, to search for all files containing the string `emil' in their names, you would have to change the line to: 435 19. First Steps with Linux newbie@earth: > find . -name "*emil*" As with all commands, we suggest you look at the manpages for more infor- mation. A very fast way to find files is with the locate command. Look at the corresponding manpage as well. If you do not want to search for a file but for a string inside a file instead, you should use the grep command (= get regular expression pattern). This command line searches for the string `detective' in the file emil: newbie@earth: > grep "detective" emil With grep, you can search vast amounts of data for certain strings very quickly. Any number of filenames can be entered. Even searching using wildcards and regular expressions can be used. grep outputs every location where the string was found. grep has a large number of options. Please consult the manpage for grep (man grep). 19.7.7 Symbolic Links By the use of symbolic links, you can give a file an additional name. This name then points to the corresponding file. Imagine that you want to keep different versions of a file, but the version actually used should always be obtainable by the same name. The solution is to use symbolic linking, which points to the version currently in use. Symbolic links behave just like the file they link, thus being executable if the "source" file is executable. The entry: newbie@earth: > ln -s check.2.4 check creates a symbolic link check which points to check.2.4. In your direc- tory, this looks like Figure 19.7.2: lrwxrwxrwx 1 newbie users 1024 Mar 21 17:13 check -> check.2.4* Screen output 19.7.2: A symbolic link using ls -l Links can be removed with the command rm. Here only the link is removed, not the file it points to! 19.7.8 Archiving and Saving Data To create and unpack archives, the command tar (tape archive) is used. Nor- mally compressed archives have the extension .tgz or tar.gz. Uncom- pressed archives have the extension .tar. The most important uses of tar are: 1. Unpacking archives (e. g., from CD's) newbie@earth: > tar xvfz archive-file.tgz tar unpacks (x) the compressed (z) archive archiv-file.tgz (f) and creates subdirectories as well, if necessary. Each file that is extracted is sent to the standard output (v). 436 19.8. Permissions 2. Creating archives newbie@earth: > tar cvfz archive- file.tgz file1 dir1 tar creates (c) the compressed (z) archive archive-file.tgz (f), where file1 and all files in dir1 are included as well. Each file that is packed is sent to the standard output (v). 3. Viewing the archive contents newbie@earth: > tar tfz archive-file.tgz tar shows a table of contents (t) of the compressed (z) archive archiv- file.tgz (f). The z flag tells tar to create/unpack compressed archives using GNU zip (gzip). newbie@earth: > tar xvf archive-file.tar unpacks the uncompressed archive-file.tar. More information can be found by typing newbie@earth: > info tar 19.8 Permissions Only the user `root', as the system administrator, has unrestricted access to all files. Permissions are structured into three categories: * Owner permissions * Group permissions * "All others" permissions Each of these categories is represented by three characters. Together with the first character (file type: d, l, or -), this results in a total length of ten for the permission flags field. Each flag is represented by a certain character. The possible flags for all three categories are the same: `r' for reading, `w' for writing and `x' for executing. If a flag is not set, this is represented by a `-' character instead of the flag. As an example, let"s look at the directory entry for the imaginary file linux.info: -rw-r-xr-- 1 newbie users 29524 Jun 29 13:11 linux.info - r w - r - x r - - Type Owner Group Others Figure 19.1: How file permissions are displayed This means that the file linux.info can be read and written by the owner (newbie). All members of the group users are allowed to read and execute it. All other users are allowed only to read it. A `-' character in the first position indicates that this is a "normal" file. The same applies to directories. If the file in question is a directory, there is a `d' in front of the nine characters (`d' stands for directory). It might look like this: 437 19. First Steps with Linux drwxr-xr-- 3 newbie users 1024 Jun 29 13:11 info/ If the flag `x' is set for a directory, then you can change to (cd) this directory. This means that users who do not belong to the group users cannot change to this directory. Changing Permissions You can change permissions with the chmod command (change mode). Gen- erally, chmod needs two arguments: * the permissions to be changed, and * a file name. The categories of the three possible groups are represented here by `u', for the owner or user, `g', for the group, and `o', for others, followed by the corresponding permissions to be changed. A `+' or a `-' sign will add or remove the corresponding permissions flag. The following command, for example, sets permissions of the file linux.info to readable, writeable (changeable) and executable for group members: newbie@earth: > chmod g+rwx linux.info If permissions for all categories are to be set, it is sufficient just to specify the permissions to be changed. The following command sets permissions to linux.info so that nobody has write permissions: newbie@earth: > chmod -w linux.info The permissions concerning reading and executing are not affected. You can give permissions and remove them within a single command line. The following command sets the permissions of linux.info to executable, not readable, and not writeable: newbie@earth: > chmod u+x-rw linux.info If you the look at the result of this, you will see: newbie@earth: > ls -l linux.info ---xr-xr-- 1 newbie users 29524 Jun 29 13:11 linux.info Two interesting commands related to chmod are chgrp (change group) and chown (change owner). See the respective manpages for more information on these commands. 19.9 Man Pages Information about commands, configuration files, and C-libraries can always be found by using the corresponding manual pages (more often referred to as "manpages"). See Table 19.5 on the facing page. Here, "keyword" is usually the command name or filename about which you need information. 438 19.9. Man Pages man Invokes the manpage for . man -f Searches for and lists the manpage found. man -k Searches for in all manpage sections and lists the manpages found (including other manpages where it is relevant). man
Invokes the manpage for from
(e. g., man from section 1). Table 19.5: Using the man command If you are using the X Window System, you may find the program xman of some use, but man is much faster. The man command uses the tool less; see also Section 19.7.3 page 434 on how to use this. If you have SuSE help installed, all manpages are available here as well, and can be comfortably read using a web browser. In Table 19.6, you can see the how the manpages are divided into different sections. 1 Describes user commands. Some of these are built-in commands. 2 System calls of libraries. 3 C library functions. 4 Description of configuration files. 5 Syntax of important files. 6 Description of games. 7 Anything that covers text, text formats, etc. 8 System administration commands. 9 Description of Linux kernel routines. n n is supposed to represent "new" programs. Here, other manpages are listed that really belong to another section, but have been placed here for reasons of convention, or because they don't quite belong in one of the other sections. Table 19.6: Manpage sections Please note that there is not a manpage for every command. If you do not find the information you are looking for in the manpages, look in the sub- 439 19. First Steps with Linux directories of /usr/doc (for example, /usr/doc/howto, /usr/doc/ howto/mini, or /usr/doc/packages). 19.10 System Information Sometimes it is necessary to gather information about the state of the system. The commands df, free, top, ps are useful for this purpose. 19.10.1 The df Command df outputs information on the hard disk space currently used. An example of the output is shown in Output 19.10.1. Filesystem 1024-blocks Used Available Capacity Mounted on /dev/sda4 699392 659258 5165 99% /home /dev/sda1 102384 23955 73310 25% / /dev/sdb1 2097136 2070485 26651 99% /usr /dev/sda3 126976 106908 20068 84% /opt Screen output 19.10.1: Output of the df command 19.10.2 The free Command free gives information on RAM and swap usage. total used free shared buffers cached Mem: 30900 29272 1628 25608 1012 6412 -/+ buffers: 28260 2640 Swap: 66524 176 66348 Screen output 19.10.2: Output of the free command 19.10.3 The Command ulimit With the ulimit (user limits) command it is possible to set limits for the use of system resources, and to have these displayed. ulimit is especially useful for limiting the memory available for applications. Through this an application can be prevented from using up too much (all) memory on its own; this could bring the system to a standstill. ulimit can be used with various options. To limit memory usage, you can use the options listed in Table 19.7 on the facing page. -m max. size of physical memory Table 19.7: continued overleaf. . . ... 440 19.10. System Information -v max. size of vitual memory (swap) -s max. size of the stack -c max. size of the core files -a display of limits set Table 19.7: ulimit: Setting resources for the user System-wide settings can be made in /etc/profile; there, creating core files must be enabled, which is needed by programmers for "debugging". A normal user cannot increase the values specified in /etc/profile by the system administrator, but he can make special entries in his own / .bashrc; see also File 19.10.1. # Limits of physical memory: ulimit -m 98304 # Limits of virtual memory: ulimit -v 98304 File contents 19.10.1: ulimit: Settings in /.bashrc Details of memory must be specified in KB. For more detailed information take a look at manpage for bash (man bash). 19.10.4 The w Command The command w shows all currently active users on your system. This com- mand outputs an informative list showing you all users, how long the system has been running, its load and what applications each user is currently using (cf. Output 19.10.3 on the following page). 19.10.5 The du Command The command du (disk usage) provides information on the space used by subdirectories and individual files. Further information can be found in the manpage for du (man du). 19.10.6 The kill Command sends signals to currently active processes ( process). It requires a process number (process ID, or PID) as an argument. This PID can be seen by using the command ps (see Section 19.10.7 on the next page). The kill command is invoked in the following way: earth: # kill 441 19. First Steps with Linux 11:19pm up 9 days, 11:13, 13 users, load average: 3.26, 2.80, 2.67 User tty from login@ idle JCPU PCPU what root tty2 2:09pm 9:10 -bash root ttyp1 :0.0 2:11pm 2 4 2 xdvi -s 3 Li root ttypa :0.0 11:19pm w root tty1 2:07pm 9:08 50 (startx) newbie ttyp0 earth.cosmos.com 11:37am 11 2 2 -bash root ttyp3 :0.0 3:24pm 4 rlogin helios newbie ttyp2 earth.cosmos.com 3:22pm 1 46 2 -bash root ttyp4 :0.0 3:27pm 1:48 8 bash root ttyp5 :0.0 5:40pm 5 1 1 telnet earth newbie ttyp6 venus.cosmos.com 3:53pm 3 5 5 -bash root ttyp7 :0.0 4:25pm 6:05 bash newbie ttyp8 helios.cosmos.com 9:37pm 1:30 1 telnet earth newbie ttyp9 helios.cosmos.com 9:50pm 1:27 -bash Screen output 19.10.3: Output of the w command If the corresponding process fails to catch the signal, it can be killed by giving the optional parameter -9. Entering: earth: # kill -9 will definitely kill the PID . 19.10.7 The ps Command ps (process status) shows the processes started by the user. More information on this command can be obtained in the manpage for ps (man ps). ps shows information on processes run by other users as well. Using the process ID (displayed by ps in the first column), it is possible to kill specific processes that are running (see Section 19.10.6 on the preceding page). 19.10.8 The pstree Command pstree illustrates the whole process tree graphically. This is shown in Output 19.10.4 on the next page. 19.10.9 The top Command This outputs all running processes and their loads, and much more besides. This list is updated periodically. You can stop it using q . 19.11 Filesystem Types in Linux ­ mount and umount 19.11.1 Overview There are a a number of filesystems available in Linux. These are shown in Table 19.8 page 444. 442 19.11. Filesystem Types in Linux ­ mount and umount init-+-bash---startx---xinit-+-X | `-sh---fvwm-+-FvwmPager | |-FvwmWinList | |-GoodStuff | |-xclock | `-xeyes |-color_xterm---bash---xdvi.bin---gs |-2*[color_xterm---bash---vi] |-color_xterm---bash---pstree |-coolmail---coolmail---xterm---pine |-cron |-gpm |-inetd |-kflushd |-klogd |-kswapd |-5*[mingetty] |-4*[nfsiod] |-sh---master---slipto |-syslogd |-update |-xload `-xosview Screen output 19.10.4: Output of pstree affs Fast Filesystem: a filesystem used on Amiga computers. ext Extended Filesystem: ancestor of the ext2 filesystem and without any significant meaning nowadays. ext2 Second extended Filesystem: standard Linux filesystem. hpfs High Performance Filesystem: the IBM OS/2 standard filesystem ­ only supported in read-only mode. iso9660 Standard filesystem on CD-ROMs. minix This filesystem originates from academic projects on oper- ating systems and was the first filesystem used for Linux. Nowadays it is used as a filesystem for floppy disks. msdos The filesystem originally used DOS, used today by various operating systems. ncpfs Filesystem for mounting Novell volumes. nfs Network Filesystem: Here, data can be stored on any ma- chine in a network and access may be granted via a network. proc Process filesystem (virtual). smb Server Message Block: used by products such as WfW, Windows NT and Lan Manager to enable files to be ac- cessed over a network. Table 19.8: continued overleaf. . . ... 443 19. First Steps with Linux sysv Used on SCO UNIX, Xenix and Coherent (commercial UNIX systems for PC's). ufs Used by BSD, SunOS and NeXTstep. Only supported in read-only mode. umsdos UNIX on MSDOS: applied on top of a normal fat filesys- tem. Achieves UNIX functionality (permissions, links, long filenames) by creating special files. It is very slow, however. vfat Virtual FAT: extension of the fat filesystem (supports long filenames). xiafs An old filesystem that is hardly used any more. Table 19.8: Filesystem types in Linux 19.11.2 Mounting and Unmounting Filesystems By means of the command mount (which can only be invoked by `root'), a storage medium can be linked into the Linux filesystem tree. Two arguments are required by mount: * name of the storage medium (corresponds to the device's name, for ex- ample, /dev/hda3) * a location (directory) where it should be attached (mounted); the directory must already exist. Option -t specifies the type of filesystem to be mounted (see Table 19.8). For example, entering: earth:/ # mount -t msdos /dev/hda2 /dosa makes the DOS partition /dev/hda2 available on /dosa; the directory /dosa must first be created (cf. Section 19.6 page 431). If specific devices often need to be attached to the filesystem at the same location, (e. g. the floppy disk drive or additional CD-ROM drives), we suggest you write an entry in /etc/fstab; see the manpage for mount (man 8 mount). Adding the -r option mounts it as read-only. Now, writing on this filesystem is not permitted. Further options are documented in the manpage for mount (man 8 mount). Invoking mount gives a list of the mounted partitions. This same list can also be seen in /etc/mtab. If mount is invoked without any argument, the contents of this list are displayed on the screen, showing all filesystems which are currently mounted. umount umount removes a partition from the available filesystems.6 6 This command used to be called unmount, but the `n' got lost somewhere in UNIX history. 444 19.12. DOS Commands in Unix with mtools As an argument to umount, you can specify either the name of the device or the name of the directory where it is mounted. So to remove /dev/hda2, which is mounted in /dosa, you can enter either: earth: # umount /dosa or: earth: # umount /dev/hda2 If you have mounted a (floppy) disk, it is important to execute the com- mand umount before you remove the disk, since not all files may have been written to disk, thus resulting in a loss of data. If there are files still open on this device, umount will try to write them back to the disk first, if this is not possible there will be a corresponding error message. If you want to "unmount" a filesystem, there should be no users working (e. g., in a shell) in directories which lie beneath the mountpoint to be unmounted; otherwise the filesystem cannot be unmounted. 19.12 DOS Commands in Unix with mtools To use MS-DOS filesystems either on floppy disk or hard disk, mtools (in package mtools, series ap ) are available. Each one of these small programs tries to emulate the corresponding DOS command as well as the original. All mtools commands are named after their DOS equivalents, prefixed with an `m', for example, mcopy. You can only use mtools commands if the disk (or hard drive) is not mounted! DOS filenames are normally formed by a device letter followed by a colon, a subdirectory, and the respective filename. To separate directories and files on DOS, only the `\' is used. With mtools, you can use either the `/' or the `\'. If you use `\' or wildcards, they have to be put into quotation marks, because otherwise they will be misinterpreted by the command line interpreter (e. g., bash). A single asterisk `*' in mtools corresponds to `*.*' in DOS. Instead of using a `/' to specify parameters, you must use the `-' sign. The standard device for mtools is `A:'. If a different one needs to be used, you must change to it by typing mcd. Don't forget to go back to the "root" directory of the device before you insert another floppy disk, otherwise no new directory tree can be read. The following (DOS) commands are currently supported by mtools: mattrib Change DOS file attributes (hidden, system, etc.). Table 19.9: continued overleaf. . . ... 445 19. First Steps with Linux mcd Change to another directory. mcopy Copy from DOS to UNIX. Always remember to specify the target. mdel Delete a DOS file. mdir List a DOS directory. mformat Assign a DOS filesystem to a low-level formatted disk. (Low-level formatting is done via the command fdformat). mlabel Rename a DOS device. mmd Create a DOS subdirectory. mrd Delete a DOS subdirectory. mread Read (low-level) a DOS file into UNIX. mren Rename an existing DOS file. mtype Show contents of a DOS file. mwrite Low-level copy a UNIX file to a DOS filesystem. Table 19.9: commands in the package mtools Disk formats that are supported are: 720 KB, 1.44 MB, or 360 KB, and 1.2 MB. Device A: is set to a 3.5" device, and device B: to 5.25". These default settings can be changed in /etc/mtools. Each entry is written in one line, as follows: * name of the drive (in DOS), e. g., a: * device file (Linux), e. g., /dev/fd0 * fat bits (12 for floppy disk drives) * number of tracks, heads and sectors By changing these parameters, you can use two 5.25" disk devices, for exam- ple. You should not enter the same DOS device letter or device name twice, however. See the manpage for mtools (man mtools) for more informa- tion. 19.13 Summary of Commands in Unix A summary of the most important commands (optional commands are given in `[]' brackets): cd directory changes to the subdirectory directory cd .. changes to parent directory cd /directory changes to the directory /directory cd changes to user's home directory cp file from file to Table 19.10: continued overleaf. . . ... 446 19.13. Summary of Commands in Unix copies sourcefile to targetfile. ln [-s] source linkname creates the [symbolic link] linkname in the current directory to source. linkname de- fines the path where the file can be found. name specifies the path where the file being sought (a kernel, for example) can be found. Only symbolic links can be created across dif- ferent filesystems. Directories can be also be linked via "symbolic links". ls [directory] lists contents of directory (brief) ls -l [directory] lists contents of directory (detailed) ls -a [directory] also lists hidden files (e. g., .xinitrc in your home directory) mkdir new directorycreates a newdirectory more file pages through a file (page down using ,page up using b ) mv file from file to moves or renames a file rm file deletes file rm -r directory recursively deletes directory. rmdir directory deletes directory (but only if it is empty) Table 19.10: Overview of the most important Linux commands `find . -name' "file" searches all subdirectories of the cur- rent directory for file. `find . -name' searches for all files containing the "*emil*" string `emil'. `man command' gives you the manpage for command. `grep string files' searches all files for the given `string' which can contain "regular expressions" as well (see Section 19.7.2 page 433 or man regexp). Table 19.11: Overview of search commands 447 19. First Steps with Linux 19.14 What Next? The usual directories where most Linux programs are stored are: * /bin * /sbin * /usr/bin * /usr/sbin * /usr/X11R6/bin The command man gives you detailed information about the command entered (see Section 19.9 page 438) and programs (provided a man page exists). Output is directed to standard output (which is normally your screen). By means of so-called pipes in the command line of the shell, symbolised by the sign `|', this output can be re-directed to a printer, or if needed, a file. For example, you want to print out the manpages for the command ls. To do this, type: newbie@earth: > man -t ls | lpr Introductions to manpages, sorted by subject, can be found in the hypertext help system of your SuSE Linux system. Just enter help. Here, other hypertext links to manpages can also be found. 448 19.15. The vi editor 19.15 The vi editor vi is preferred to other editors by system administrators because it is small and fast, will run on any kind of terminal, yet is very powerful, efficient and flexible. Its commands enable you to perform any editing task quickly without having to leave the main keyboard. It is the one editor you will find on every UNIX installation. vi comes as a standard in Linux. For those trained on other editors, vi takes a little getting used to. This chapter will enable you to do basic editing using vi. See [Lam90]. There are three modes available in vi: * Command mode. Every pressed key is interpreted as part of a command. * Input mode. Pressed keys are input as text. * ex mode. Enables vi to interact with the shell in very powerful and sophisticated ways. We will discuss only a few elementary commands. vi starts in Command mode. You can change from Command to Input mode by pressing one of the editing mode keys shown in Table 19.12 on the next page. To change from Input mode back to Command mode, just press Esc . The basic commands of Command mode are: `j' moves cursor down one line `k' moves cursor up one line `h' moves cursor left one column `l' moves cursor right one column `CTRL-f' moves cursor down one screen `CTRL-b' moves cursor up one screen `G' moves cursor to end of document `nG' moves cursor to line n `w' move cursor forward one word `b' move cursor back one word `0' move cursor to start of line `$' move cursor to end of line `i' changes to input mode (characters are inserted at the current cursor position) `a' changes to input mode (characters are inserted after the current cursor position) `A' changes to input mode (characters are appended at the end of the current line) `R' changes to input mode (replaces and overwrites old text) `r' changes to input mode (overwrites the one character currently under the cursor `C' changes to input mode (rest of line is replaced by the new text) Table 19.12: continued overleaf. . . ... 449 19. First Steps with Linux `o' changes to input mode (after the current line a new line is added for text insertion) `O' changes to input mode (before the current line a new line is added for text insertion) `x' deletes the current character (and puts in buffer) `dd' deletes the current line (and puts in buffer) `dw' deletes to the end of the current word (and puts in buffer) `cw' changes to input mode (rest of the current word is overwritten by the input) `yy' copy current line into buffer `p' paste text in buffer after cursor position `P' paste text in buffer before cursor position `u' undoes the last command `J' appends following line to current line `.' repeats the last command `:' changes to ex mode Table 19.12: Basic commands of vi All commands can be preceded by a number. This number sets how many times the given command should be executed. Thus, entering `3dw' deletes three words at once and `10x' deletes 10 characters. `20dd' deletes 20 lines. The most important commands in ex mode are: `:q!' quits vi without saving changes `:w [file]' saves in [file] `:x' saves changed file and quits `:e [file]' edits [file] Table 19.13: Elementary ex mode commands of vi 450