Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Ubuntu Linux - From Novice To Professional (2006)

.pdf
Скачиваний:
71
Добавлен:
17.08.2013
Размер:
25.54 Mб
Скачать

238

C H A P T E R 1 4 U N D E R S T A N D I N G L I N U X F I L E S A N D U S E R S

However, du is limited by the same file permission problems as the find tool, as shown in Figure 14-6. If you run du as an ordinary user, it won’t be able to calculate the total for any directories you don’t have permission to access. Therefore, you might consider prefacing the command with sudo.

Figure 14-6. The du command shows the size of a file, and the df command can be used to gauge the amount of free space on the disk.

Finding Out the Amount of Free Space

What if you want to find out how much free space is left on the disk? In this case, you can use the df command. This command is also demonstrated in Figure 14-6.

The df command works on a partition-by-partition basis. Typing it at the command prompt will show you how much space is free on the entire disk. Once again, you can add the -h option to the df command to have the file sizes returned in megabytes and gigabytes (and even terabytes if your hard disk is big enough!).

The df command also returns information about something called tmpfs. These are temporary file stores and you can ignore them.

Note There is as much space free in any directory as there is space on the disk, which is why df displays data about the entire partition. If you’re using a system managed by a system administrator within a business environment, you might find that quotas have been used to limit how much disk space you can take up.

However, if you’re using a desktop PC and are the only user, this won’t be activated.

C H A P T E R 1 4 U N D E R S T A N D I N G L I N U X F I L E S A N D U S E R S

239

Summary

In this chapter, we examined how the Ubuntu file system lies at the heart of an understanding of how the operating system works. We also discussed how the file system and user accounts go hand-in-hand and are inextricably linked. This involved discussing the concept of file ownership and usage permissions, plus how these can be manipulated using command-line shell tools.

We also discussed the overall structure of the Ubuntu file system and how external file systems can be mounted and made available within Ubuntu. Finally, we looked at how to find files and how to gauge how much free space there is within the file system.

In the next chapter, we’ll look at how the BASH shell can be used to view and otherwise manipulate text files, which are also important to the way Ubuntu works.

C H A P T E R 1 5

■ ■ ■

Working with Text Files

Windows views text files as just another file type, but to Ubuntu, they can be essential components that make the system work. Configuration files are stored as plain text, and program documentation is also stored as text. This is clearly different from Windows, where it’s very likely any information you’re supposed to read will be contained in a Windows Help file, a rich text format (RTF) file, or even a Microsoft Word document.

Because of the reliance on text files, the shell includes several commands that let you display, edit, and otherwise manipulate text files in various ways. Learning to use the shell, and therefore learning how to administer your Ubuntu system, involves having a good understanding of these text tools. You’ll use text tools for editing configuration files and viewing log files, as just two examples.

Viewing Text Files

You can easily view files using command-line tools, including cat, less, head, and tail. The simplest command for dealing with text files is cat.

Using the cat Command

When followed with a filename, the cat command will display the text file on screen:

cat mytextfile

cat is short for concatenate, and it isn’t designed just to display text files. That it can do so is simply a side effect of its real purpose in life, which is to join two or more files together. However, when used with a single file, it simply displays its contents on screen.

If you try to use cat, you’ll realize that it’s good for only short text files; large files scroll off the screen.

Using the less Command

Because cat works well only with short files, and to give you more control when viewing text files, the less and more commands were created. The more command came first but was considered too primitive, so someone came up with less, which is preferred by many Linux users. However, both are usually available on the average Linux installation.

241

242

C H A P T E R 1 5 W O R K I N G W I T H T E X T F I L E S

Let’s look at using less to read the OpenOffice.Org README file, which contains information about the current release of the office suite. The file is located at /usr/lib/openoffice2/README, so to use less to read it, type the following:

less /usr/lib/openoffice2/README

You can scroll up and down within the less display by using the cursor keys. If you want to scroll by bigger amounts of text, you can use the Page Up and Page Down keys. Alternatively, you can use the spacebar and B key, both of which are commonly used by old-hand Linux users for the same function. In addition, the Home and End keys will take you to the start and end of the document, respectively.

When using less, keep an eye on the bottom part of the screen, where you’ll see a brief status bar. Alongside the filename, you’ll see how many lines the document has and which line you’re currently up to. In addition, you’ll see as a percentage the amount of document you’ve already read through, so you’ll know how much is left to go.

less lets you search forward through the file by typing a slash (/), and then entering your search term. Any words that are matched will be highlighted on screen. To repeat the search, type n. To search backward in a file from your current point, type a question mark (?). To quit less, simply type q.

Although it’s supposedly a simple program, less is packed with features. You can see what options are available by reading its man page or by typing less --help.

Using the head and tail Commands

A couple of other handy commands that you can use to view text files are head and tail. As their names suggest, these let you quickly view the beginning (head) of a file or the end (tail) of it.

Using the commands is simple:

tail mytextfile

or

head mytextfile

By default, both commands will display ten lines of the file. You can override this by using the -n command option followed by the number of lines you want to see. For example, the following will show the last five lines of mytextfile:

tail -n5 mytextfile

These two commands are very useful when viewing log files that might contain hundreds of lines of text. The most recent information is always at the end, so tail can be used to see what’s happened last on your system, as shown in the example in Figure 15-1.

Although they’re powerful, all of these shell commands don’t let you do much more than view text files. If you want to edit files, you’ll need to use a text editor such as vi.

C H A P T E R 1 5 W O R K I N G W I T H T E X T F I L E S

243

Figure 15-1. The tail command can be very useful for viewing the last few lines of a log file.

STANDARD INPUT AND OUTPUT

If you’ve read any of the Ubuntu man pages, you might have seen references to standard input and standard output. Like many things in Ubuntu, this sounds complicated but is merely a long-winded way of referring to something that is relatively simple.

Standard input is simply the device that Ubuntu normally takes input from. In other words, on the majority of desktop PCs when you’re using the command-line shell, standard input refers to the keyboard. However, it’s important to note that it could also feasibly refer to the mouse or any other device on your system capable of providing input; even some software can take the role of providing standard input.

Standard output is similar. It refers to the device to which output from a command is usually sent. In the majority of cases at the command line, this refers to the monitor screen, although it could feasibly be any kind of output device, such as your PC’s sound card and speakers.

The man page for the cat command says that it will “concatenate files and print on the standard output.” In other words, for the majority of desktop Ubuntu installations, it will combine (concatenate) any number of files together and print the results on screen. If you specify just one file, it will display that single file on your screen.

In addition to hardware devices, input can also come from a file containing commands, and output can also be sent to a file instead of the screen, or even sent directly to another command. This is just one reason why the command-line shell is so flexible and powerful.

244

C H A P T E R 1 5 W O R K I N G W I T H T E X T F I L E S

Using a Command-Line Text Editor

A variety of text editors can be used within the shell, but three stand out as being ubiquitous: ed, vi, and Emacs. The first in that list, ed, is by far the simplest. That doesn’t necessarily mean that it’s simple to use or lacks powerful features, but it just doesn’t match the astonishing power of both vi and Emacs. To call vi and Emacs simple text editors is to do them a disservice, because both are extremely powerful interactive environments. In particular, Emacs is considered practically an operating system in itself, and some users of Linux treat it as their shell, executing commands and performing everyday tasks, such as reading and sending e-mail from within it. There are entire books written solely about Emacs and vi.

Tip A fourth shell-based text editor found on many Linux systems is nano. This offers many word processor-like features that can be helpful if you’ve come to Linux from a Windows background.

The downside of all the power within Emacs and vi is that both packages can be difficult to learn to use. They’re considered idiosyncratic by even their most ardent fans. Both involve the user learning certain unfamiliar concepts, as well as keyboard shortcuts and commands.

Although there are debates about which text editor is better and which is best, it’s generally agreed that vi offers substantial text-editing power but isn’t too all-encompassing. It’s also installed by default on Ubuntu. On Ubuntu, Emacs must be installed as an optional extra. Both text editors are normally available on virtually every installation of Linux or Unix. We’ll concentrate on using vi here.

It’s important to understand that there isn’t just one program called vi. There are many versions. The original vi program, supplied with Unix, is rarely used nowadays. The most common version of vi is a clone called vim, for vi improved, and this is the version supplied with Ubuntu. However, there are other versions, such as Elvis. Most work in a virtually identical way.

Note There’s always been a constant flame war between advocates of vi and Emacs, as to which is better. This could be quite a vicious and desperate debate, and the text editor you used was often taken as a measure of your character! Nowadays, the battle between the two camps has softened, and the Emacs versus vi debate is considered an entertaining cliché of Linux and Unix use. Declaring online which text editor a user prefers is often followed by a smiley symbol to acknowledge the once-fevered emotions.

Understanding vi Modes

The key to understanding how vi works is to learn the difference between the various modes. Three modes are important: Command mode, Insert mode, and Command-Line mode.

C H A P T E R 1 5 W O R K I N G W I T H T E X T F I L E S

245

Command Mode

Command mode is vi’s central mode. When the editor starts up, it’s in Command mode, as shown in Figure 15-2. This lets you move around the text and delete words or lines of text. vi returns to Command mode after most operations. In this mode, the status bar at the bottom of the screen shows information such as the percentage progress through the document. Although you cannot insert text in this mode, you can delete and otherwise manipulate words and lines within the file. You can also move through the text using the cursor keys and the Page Up and Page Down keys.

Figure 15-2. In vi, the main mode is Command mode.

Table 15-1 shows a list of the commands you can use in Command mode (consider photocopying it and sticking it to the side of your monitor as a handy reference).

Table 15-1. vi Command Mode Commands

Command

Description

Delete Text

 

dd

Delete current line

ndd

Delete n number of lines (for example, 5dd will delete five lines)1

dw

Delete the current word under the cursor 2

db

Delete the word before the cursor2

D

Delete everything from the cursor to the end of the line1

246

C H A P T E R 1 5 W O R K I N G W I T H T E X T F I L E S

Table 15-1. vi Command Mode Commands (Continued)

Command

Description

Search

 

/

Search forward (type the search text directly after the slash)

?

Search backwards

n

Repeat search in a forward direction

N

Repeat search in a backward direction

Cut and Paste

 

yy

Copy the current line3

nyy

Copy n number of lines into the buffer from the cursor downwards (for example,

 

5yy copies five lines of text)

p

Paste the contents of the clipboard3

Insert Text

 

i

Switch to Insert mode at the cursor

o

Switch to Insert mode, placing the cursor below current line

O

Switch to Insert mode, placing the cursor above current line

A

Append text to end of line

Navigation4

 

$

Move the cursor to the end of the current line

w

Move the cursor to the next word

b

Move the cursor to the previous word

Miscellaneous

 

.

Repeat the last command

u

Undo the last command

1A line ends where a line-break control character occurs in the file. Because of this, a line of text may actually take up several lines of the on-screen display.

2This will delete the remainder of current word before/after the cursor if the cursor is in the middle of a word.

3The standard documentation refers to copying as "yanking” and the clipboard as the “buffer.”

4You can also use the cursor keys to move around the file and the Page Up and Page Down keys to move up and down a page at a time. Additionally, press 0 (zero) on the main keyboard, not the numeric keypad, to move the cursor to the start of the current line, or Shift+0 to move forward one sentence (until the next full stop).

Insert Mode

To type your own text or edit text, you need to switch to Insert mode. This is normally done by typing i, but you can also type O or o to change to Insert mode, which is indicated by the word INSERT appearing at the bottom of the screen, as shown in Figure 15-3. The difference between

C H A P T E R 1 5 W O R K I N G W I T H T E X T F I L E S

247

the commands required to switch into Insert mode is that some let you insert before or after the cursor. Generally, i is most useful, because what you type will appear before the character under the cursor, as with most word processors. The commands that activate Insert Mode are listed in Table 15-1, under “Insert Text.”

Tip By typing A (Shift+A), you can add text to the end of the line on which the cursor currently resides.

Figure 15-3. Use vi’s Insert mode to add and edit text.

In Insert mode, you can still move around the text using the cursor keys. Anything you type will appear the point of the cursor. To quit this mode, press the Esc key. This will return you to Command mode.

Command-Line Mode

The third mode you should be aware of is Command-Line mode (note that, irritatingly, this is not the same as the Command mode). As its name suggests, this is the mode in which you can enter commands to save and load files, as well as perform other fundamental tasks to control vi or to quit the program. You can enter Command-Line mode by typing a colon (:), although if you’re in Insert mode, you’ll first need to leave it by pressing the Esc key. You can identify when vi is in this mode because the cursor will be at the bottom of the screen next to a colon symbol, as shown in Figure 15-4. To quit Command-Line mode, press the Esc key. You’ll be returned to Command mode. Note that you’ll automatically leave Command-Line mode after each command you issue has completed.