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 only good for 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.

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 /opt/OpenOffice.org/share/readme/README01, so to use less to read it, type the following:

less /opt/OpenOffice.org/share/readme/README01

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

There are a couple of other handy commands that you can use to view text files: 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 16-1.

image from book
Figure 16-1. tail can be very useful for viewing the last few lines of a log file.

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.

image from book
Standard Input and Output

If you've read any of the SUSE Linux man pages, you might have seen references to standard input and standard output. Like many things in SUSE Linux, 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 SUSE Linux 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 SUSE Linux 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 straight to another command. This is just one reason why the command-line shell is so flexible and powerful.

image from book




Beginning SUSE Linux from Novice to Professional
Beginning SUSE Linux: From Novice to Professional
ISBN: 1590594584
EAN: 2147483647
Year: 2005
Pages: 293
Authors: Keir Thomas

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net