|
| |||
| ||||
|
Chapter 7
Command Line Operations
Many newcomers to Linux will wonder why they should read anything about command line operations. After all, doesn't X Windows shield you from the command line? The answer of course is yes--but learning command line basics does have several advantages:
This chapter focuses on the basics:
Basic operations at the OpenLinux command line are almost identical to UNIX, and not dissimilar to DOS.
ls, cd and pwd are the basic navigation commands.
ls, or list screen, at the command line gives a list of all 'non-hidden' files and directories at that directory level (Figure 7-1).
NOTE: Remember, all commands in Linux are case-sensitive. In other words, cd is not the same as CD or Cd. The same is true for the names of files and directories.
Figure 7-1 Output of ls Command at the Root Directory
To see the size of each file in bytes, add -l to the ls command, as so:
The byte format, however, is not always that easy to read, especially without commas. A more 'human readable' format is obtained by appending -h to the command. Thus,
at the root directory gives a printout similar to Figure 7-2.
Figure 7-2 Output of ls -l -h
The ls command has another very common option, to display hidden files. That will be presented shortly.
cd, or change directory, moves the view to some other point in the directory structure. If, for example, in the above example in Figure 7-1, you wanted to move to root, which is one level down, you would enter:
Use the slash / to go back from any level in the directory system to the system's root. As an example, if in the example above you entered
and pressed the Enter key, you would be returned to:
The slash is also used to move down more than one level. If, for example, you wanted to move down two directory levels, to the /etc/X11 sub-directory, you would enter:
and press the Enter key. Note the spacing. There is no space between the slash and etc, but there is a space between the cd command and the slash. At the command line, spaces can be as important as case.
The slash can be used to move down as many levels as you like, with only one command. For instance, in order to explore documents available on specific graphic adapters, you might want to enter:
If from this rather remote directory you wanted to go back just one level, you could enter:
The two dots, or periods, after the cd, direct it to move up only one level at a time. Notice again the importance of spacing: no spaces between the periods, but one space between the cd and the first period.
You could use a combination of dots and slashs to move up two directories at a time. If, as an example, you wanted to move from /usr/X11R6/lib/X11, up to /usr/X11R6 with one command, you could enter:
Table 7-1 shows just some of the various options for the cd command.
Command | Result |
---|---|
cd / | move up from whatever level to the root directory |
cd X | move down (one level only) to directory X |
cd .. | move up one level at a time |
cd X1/X2/X3 | move down as many levels as you like to directory X# |
cd ../.. | move up two levels at a timethis is an extendable pattern |
cd /X | move laterally to a different branch of the directory tree |
As the last row of Table 7-1 shows, preceding a directory with an slashand this is also an extendable patternwill move the view to any directory or subdirectory, even on a different branch of the directory tree.
As an example, to reach /root from /usr/X11R6, simply enter:
and press the Enter key.
To come back for a moment to the ls command, there is one more common option to explore. Files are 'hidden' in Linux through the simple method of including a period as the first character in a file name. The -a option is used to reveal such hidden files. Using the ls command at the /root directory will probably discover few filesperhaps only one. But if you enter:
the -l option will reveal many hidden files.
Finally, the pwd command stands for print working directory. As the examples above have made clear, the command prompt by default only shows the current working directory, and one can easily get somewhat lost when moving from one remote directory to another. The pwd command shows in full precisely where the user is in the directory system.
cp, mv and rm are the basic file manipulation commands.
cp copies a file or files from one location to another. As an example, a user at the root directory (/), wanting to copy a file named test from the root directory to the /etc directory, would type:
The original copy of the file stays in its original location; a copy is made in the new location. The user does not have to be at the exact location of the file they wish to copy: the following command, issued from the root directory, will succeed in copying the ppp-on file from a remote directory to the floppy disk (notice the space between the cp command and the following slash, and the end of the file being copiedppp-onand the first slash of the destination directory):
The mv command is used to move a file, as its name suggests. Its syntax is identical to cp, but instead of leaving the original file intact, the mv command deletes it.
mv can also be used to rename a file. An example: if the following command is given:
The result is to delete the original file, defaultFontPath, moving it to the new directory / and simultaneously renaming it to fontpath.
The rm command is used to delete files.
NOTE: rm deletes files permanently! There is no going back, no file retrieval possible when it has been deleted with rm.
The syntax for rm is quite simple. Wanting to remove the file XF86Config from the /etc directory, the user would simply type:
and press the Enter key.
locate, updatedb, and clear are all useful commands. The first two aid in locating files, and the last clears up the mess on the screen made by all the other commands.
The locate command locates files. The syntax is straightforward: to find a file, for instance, named XF86Config, enter:
Figure 7-3 shows the output.
Figure 7-3 Output of Locate XF86Config
Notice that the command displays any file with 'XF86Config' anywhere in the name. This means that the resulting output can sometimes be quite long, depending on the file name. It can easily scroll right off the screen. There are two ways of dealing with this: using the command in a terminal emulation window within KDEand using the window's vertical scroll bar to explore the outputor by combining locate with | less. The less command is covered below.
Locate works by searching a special file name database on your system. That database is updated automatically on a daily basis. However, there may be times, after additional program installation for instance, when you need to use the locate command, and you know that the database hasn't yet been updated. In that case you can update it manually by typing:
and pressing the Enter key.
Finally there will be times when you simply want to clear the screento delete lists of commands and output. To achieve this, enter clear. The screen will be wiped, except for the command prompt in the upper left-hand corner.
halt, reboot, and shutdown are all commands to shutdown OpenLinux at the command line.
halt performs an orderly shutdown of OpenLinux, then turns off the computer.
reboot performs the same orderly shutdown, but then reboots the computer.
shutdown is a more complex command, and can be used with a number of options. Using it with -h halts the computer, with -r reboots the computer. Two of the most useful options are -F and the time argument.
shutdown -F forces a check of the filesystem the next time the computer is turned on. This can be useful if you suspect file or directory corruption.
The time argument has a number of different formats. One of the simplest, and most commonly used, is the word, now. As an example of how these options can be used in tandem, the following command would perform an immediate but orderly shutdown, followed by a reboot of the computer, and a check, during startup, of the filesystem.
Another use of the time argument is a + sign, followed by the number of minutes to wait until shutdown. The command:
would result in the following warning message:
The system is going DOWN for reboot in 3 minutes !!
A similar warning message would be presented in two minutes, and then at the three minute mark the system would shutdown and reboot.
Configuration Commands
Useful configuration commands include: mkdir, rmdir, mount and umount, and eject.
The mkdir command is used to make directoriesthe syntax is simply the command, followed by the name of the new directory, such as:
This command would make a new directory, C, within the current directory.
The rmdir command does just the opposite. The command:
would delete the directory made by the first command.
OpenLinux handles access to floppy disk drives and CD-ROMs quite differently than certain other operating systems (at least at the command line. Within KDE, access is similar to other operating systems). When working at a terminal, simply inserting a floppy or CD is not enoughthe operating system can't read them until they are manually 'mounted'.
The mount and umount commands are used to mount and unmount devices such as diskettes and CD-ROMS, as well as partitions belonging to separate filesystemssuch as Windows' partitions.
As an example, to access a floppy disk at the command line, first insert the floppy, then type:
and press the Enter key. At this point, use the cd command to change to the floppy directory:
The diskette is mounted and ready to be used.
The CD-ROM is mounted in exactly the same way, and even from the same /mnt directory:
Insert floppy 1.
Mount floppy 1.
Unmount floppy 1.
Eject floppy 1.
Insert floppy 2.
Mount floppy 2, etc. The umount command is used to unmount disks and partitions. The syntax is identical to mount. An example would be:
The mount and umount commands can sometimes confuse PC hardware, especially the CD-ROM drives. In such situations, the eject command can be very useful. Simply typing it at the command lineit takes no arguments and no optionsand pressing the Enter key, will eject the CD.
Common tools to check and to maintain the performance of the operating system are df, free, ps, top, and kill.
All five of these commands come from the UNIX world, and all three are industrial-strength system tools. As with all the commands in this chapter, they can be run either at the console or at a virtual terminal within KDE.
df stands for disk free, and is used to check the amount of free space on mounted disks, partitions and removable media. Figure 7-4 shows a typical example of this command. In this figure, there are a total of four mount points active: the first is the main Linux partition /. The second, /HP, is a Samba mount point (as can be seen from its filesystemsee Chapter Thirteen for more information on Samba). The third is the floppy disk, and the fourth is a mounted Windows partition.
The df command is useful not only for checking the amount of free space, but also for checking the status of various mount points. It is easy to forget, for instance, whether or not the floppy is still mounted, and it shouldn't be ejected if the mount point is still active. Check with df before ejecting is a good idea. Samba mount points can for various reasons become unstable; it is a good idea to check them regularly. And of course some filesystems should for reasons of data security only be mounted when necessary; df can be regularly used to check their status.
mkdir C
rmdir C
mount /mnt/floppy
cd /mnt/floppy
cd /mnt/cdrom
NOTE: It is important not to change floppy disks when they are mounted. Doing so can confuse the operating system. The rule is: insert and eject removable media in OpenLinux only when they are unmounted. Thus, the procedure for checking two floppies would be:
umount /mnt/floppy
Figure 7-4 Output of the df Command
The free command is used to check for free system memorynot storage memory, but rather RAM memory. An example of the output of this command is in Figure 7-5.
The meaning of these numbers is not as obscure as it may first appear. The first line is fairly straightforward. Total is the total amount of RAM on the system. Used is the total amount of RAM being used in one way or another. Free memory is simply used subtracted from the total.
Buffers and cache both refer to RAM reserved for data or code which, although perhaps not currently in use, would probably speed up the system by being kept in system memory.
The -/+ buffers/cache line is a useful display of potential resources, for both buffers and cache memory can be dynamically reallocated when additional RAM is required. As a result, the potential free RAM on this system, for instance, is actually 17,312MBthe current free RAM plus the memory currently allocated for buffers and cache.
Figure 7-5 Output of the free Command
ps stands for process status, and is used to quickly check running processes and their process IDs. A process can be any running program, including programs running in the background. Each program has its own identification numberthe process ID, or PID.
Troubleshooting is the most common reason to use ps. Typical scenarios are:
ps is usually run with the -e option, to give a complete picture of all processes on the system. Figure 7-6 shows typical output of the ps command (the question marks indicate a background process not running on a particular terminal).
Figure 7-6 Output of the ps -e Command
Whereas ps gives you a one-time snapshot of running programs, top gives you a running view of programs, their PIDs, and also the percentage of CPU time and RAM which they are using. This is an excellent tool for the same three trouble-shooting scenarios shown above, as well as another: examining the system for the errant program that is hogging the CPU. An illustration of output from top is shown in Figure 7-7. On the left is the PID and the program's user. On the right is the percentage of CPU time and memory that each process is using. The actual name of each process is at the far right.
Figure 7-7 Output of the top Command
kill is the aptly-named program that kills off processes and programs. The user might want to kill off programs because, although defunct, they refuse to die and release their resources; or, having gone completely out of control, they are consuming the system's resourcesthe CPU, RAM, or bothand refuse to shut down normally. These are occasionalhopefully rareproblems in all operating systems. OpenLinux makes it easy to deal with them.
kill is generally used in conjunction with ps. First, the PID of the offending program is found with ps, then it is shutdown with kill. An example would be:
top also has a kill feature built-in. Simply enter:
and then the PID of the offending program. You will be prompted for the signal. Just pressing Enter uses signal 15, which performs a civilized execution. However, not all miscreants will succumb to a signal 15. If the process refuses to die, enter signal 9. This is a signal no process can resist; its inevitable result is sudden death.
less and the vi Editor
less is a text file reader for use at the console or virtual terminal. Its usage is simple: enter less followed by the name of the file you wish to read. Use the up and down arrow keys to move through the file. If you wish to scroll through a long file more quickly, use the Page Up and Page Down keys.
To exit the less file reader, type q and press the Enter key.
less can be used in conjunction with the locate command and the pipe character ( | ) to produce easily read output, very useful when the locate command alone produces too many lines of output to be read at a regular terminal. As an example, when trying to find a file with the name of netscape in it, the locate command will produce an output that scrolls past several screens. To better examine the output, enter:
vi is a text file editorone of the oldest around. It is a far cry from editors designed for use in a graphical user environment. However, it is fast, and very easy-to-read at a terminal.
vi has two modes: input and command. The editor starts up in the command mode, and enters the input mode when the user presses the Escape key. To reenter command mode, press the Escape key again. An example will make this clearer.
To create and edit a file named 'test', enter:
The screen should appear completely blank, save for a column of tildes on the left-hand side, and at the lower left-hand corner the words: "test" [New File]. The user is now in command mode. To enter input mode, press the Escape key. You should hear a beepconfirmation that the mode has changed.
The input mode is used to edit the file. To insert a line, press the i key. The word INSERT should appear at the bottom of the screen. At this point text can be entered or deleted in much the same way as in any text editor. Enter a line of text. To save it, leave the input mode by pressing the Escape key again, and save the file by pressing the colon key, then w (for write), and the Enter key.
To leave the editor, press the colon key again, then q and Enter.
The table below summarizes some of the most common vi commands.
The Internet at the Command Line
The Internet at the command line is generally faster than it is within X Windows, or any other graphical user interface. Two common Internet programs which are often used to take advantage of this extra speed are Lynx and ftp.
Lynx is a text-only Web-browser. It is completely command line oriented, using only the keyboard for control and navigation. As an example of how to use this program, connect to the Internet, and at the command line enter:
Within moments a screen similar to Figure 7-8 should appear.
Figure 7-8 www.calderasystems.com From Within Lynx The instructions at the bottom of the screen direct the user to use the Up and Down arrow keys to move through the document. Moving in this way down to the next page, and pressing Enter when the link for OpenLinux 2.3 is highlighted, takes you to the full-text Web page shown in Figure 7-9.
This is not only considerably faster than a graphical Web-browser, it can also be easier to read at a console.
Figure 7-9 A Full Text Screen in Lynx Ftp, or File Transport Protocol, is a method used to transfer files across the Internet. Most users will have used this protocol at one time or another while downloading files onto their computers from within their Web browsers. However, ftp can be used at the command line without graphical overhead, and run in the background, even allowing further Web-browsing in the foreground while the file downloads.
The one disadvantage of ftp at the command line is navigation. Moving through a foreign directory tree can be frustrating and tedious. It is best to use this method of downloadingand uploadingfiles, when you know exactly where the file in question is coming from or going to.
A sample ftp session could be launched to retrieve the most recent version of a screen saver named xsnow. First, establish an Internet connection, then enter:
When asked to login, enter the login: anonymous. When asked for a password, enter your e-mail address.
Navigation commands during an ftp session are very similar to the normal navigation commands at a regular command prompt. The xsnow.rpm file resides three directories down; to reach it enter:
To check that the file is still there, use the ls command. To download it, enter:
When finished, close the ftp connection by typing: exit. Note that this closes the ftp connection; it does not close your Internet connection.
locate netscape| less
vi test
Command
Result
vi (file name)
Starts the vi editor and opens the file.
Escape key
Switches modethe first time, from command mode to input mode.
i
Inserts text from within input mode.
x
Deletes text from within command mode.
dd
Deletes a line from within command mode.
u
reverses the change from within command mode.
:w and Enter
Writes a file in command mode.
:q and Enter
Quits the editor in command mode.
lynx www.calderasystems.com
ftp sunsite.unc.edu
cd /pub/linux/games
get xsnow.rpm
NOTE: For a list of the most commonly used Linux commands, see Appendix D.
Copyright © 2000 Caldera Systems, Inc. All rights reserved Legal Notice | Privacy Notice