By Heike Jurzik
You don't always need to launch an editor to view text files at the console. More often than not, a simple viewer is far quicker. But the programs we will be looking at in this month's issue have a lot more tricks up their sleeves. If you are looking for a fast approach to concatenating multiple files in a single file, cat is the tool you need. This article also looks at tips for the more and less pagers, showing you more convenient approaches to using these tools. And if you are just interested in grabbing the first or last few lines of a file, head and tail are the tools you need.
cat lets you output a text file via standard output:
To add line numbers to this output, just add the -n switch. If you attempt to output a binary file with cat, rather than a text file, the shell might display all kinds of weird characters after the event. To reset the display, just type reset at the prompt - you can do this blindfold, if necessary.
But cat can do more than that. The program's name is derived from the verb "to concatenate," and cat really does let you "glue" files together. To concatenate two text files in a single file, just use cat and the > operator:
cat file1 file2 > file3
Some caution is advisable if file3 exists - cat will just overwrite the existing file. But you can use the >> operator instead to tell cat to append both files to the third file:
cat file1 file2 >> file3
If you need to view a text file in the shell, with the ability to scroll and search in the file, a pager like more or less is a more useful choice. To view a file page by page, enter:
Press the space key to scroll down, and the enter key to scroll up one line. A status line at the bottom of the page shows you the position of the current page as a percentage of the whole file. [B] ("back") scrolls one page back, [H] displays the help, and [Q] ("quit") quits the program. more quits automatically when you reach the end of the file.
more has a few more practical keyboard shortcuts: if you are interested in the current line number, press the equals sign (=), the combination of [Shift+;], [F] (:f) displays the filename along with the line number. To search within the file, type a slash character (/) and enter the search key.
more can display multiple text files back-to-back if needed: to do this, simply pass all the file names to the tool when you launch it. The pager indicates the name of each file:
:::::::::::::: wget.txt ::::::::::::::
You can press :n (for "next") or :p (for "previous") to jump from file to file. The :p command first takes you to the start of the current file first; pressing :p a second time takes you back to the previous text file.
There is no need to quit more and launch your favorite editor to edit a file. Instead, just press [V] while viewing the file. This launches the vi editor by default, and you can then proceed to modify the file. If you would prefer more to call a different editor, just modify the value of the EDITOR environmental variable, which is evaluated by many other programs. For example, if you enjoy working with the Xemacs editor, you can define the variable with the following command:
Quitting the editor takes you back to more. To permanently modify the value of the EDITOR environment variable, add the export command to your bash configuration file, ~/.bashrc, and reparse the file as follows:
Options let you influence the way more reacts; you can specify these options when you launch the tool. For example, if you need to edit a file with a lot of empty lines, you can enter
more -s file
to tell the pager to "shrink" multiple empty lines to a single empty line. To restrict the view to just ten lines in the current window, add -10 as a start option - and to prevent more from forgetting this setting when you scroll through the file, add the -p option.
If you would like more to use the same command line parameters each time you run the tool, you can set the MORE environmental variable and save a lot of typing. To do so, add the following (or similar) to your bash configuration file, ~/.bashrc:
export MORE="-10 -p -s"
and then run the following command to reparse the file:
less is more`s successor: it recognizes the same keyboard shortcuts, but adds a few functions of its own. If you prefer not to use the space key and [B] for scrolling, you can just use the arrow keys instead. [G] jumps to the start of the file, and [Shift+G] to the end of the file. less not only supports forward searching, (/), but also backwards searching via the question mark.
In contrast to more, the less pager does not automatically quit when you reach the end of the file; instead you have to quit the program explicitly by typing [Q]. Whereas more does not tidy up the display when you quit, and will thus display the last piece of text on screen, less leaves with a tidy screen. You can change this behavior if you like by setting the -X command line parameter. Just like with more, you can define an environmental variable for less to specify your preferred default parameters. For example, to use the -X option permanently, add the following line to your ~/.bashrc:
Just like the more pager, less parses the EDITOR variable and launches the text editor the variable points to when you press [V]. The keyboard shortcuts for viewing multiple files are also identical. If you pass multiple files to the pager, you can press :n to move to the next file and :p to go back to the previous file. In contrast to more, less is so kind as to remember the position in the file where you typed the command.
Another practical feature of the less command, if you are working with multiple files, is that less shows you the next file name in the status line at the bottom of the page.:
textfiles.txt (file 1 of 2) (END) - Next: spellcheck.txt
To avoid the need to press :p to move to the previous file, you can set the -e option when launching less. This option tells less to automatically move to the next file when you reach the end of the current file and press the space key, [Down arrow], or [Page Down].
less has a practical feature for monitoring file changes. If you press [F] in less, the pager shows you recently added lines. This is extremely practical while using less to view logfiles - the status line displays Waiting for data... (interrupt to abort) to show that less is following changes (Figure 1). [Ctrl+C] quits "follow" mode and returns to the normal pager functions.
The zless and zmore commands support viewing of zipped files; the tools give you the same keyboard shortcuts as the legacy pagers. Instead of using a complicated command sequence such as:
zcat Xinerama-HOWTO.gz | less
you can just type
to read the compressed file on your screen.
If you are just interested in the first or last few lines of a text file, head and tail give you a faster approach:
displays the first ten lines of a file.
tail is the head counterpart for the end of the file. Like head, tail can handle multiple filenames as input.
tail has many practical extras: -f is probably the most commonly used. Like with the less pager, this option tells tail to enter "follow" mode and log any changes. This is useful for monitoring logfiles; the following command:
tail -f /var/log/messages
sends tail into an infinite loop, where it monitors a file for changes and displays the latest entries. To quit "follow" mode, press [Ctrl+C].