CompTIA Linux+ Powered by Linux Professional Institute Study Guide. Richard Blum

Читать онлайн книгу.

CompTIA Linux+ Powered by Linux Professional Institute Study Guide - Richard Blum


Скачать книгу
this goal with the head command, which echoes the first 10 lines of one or more files to standard output. (If you specify multiple filenames, each one's output is preceded by a header to identify it.) You can modify the amount of information displayed by head in two ways:

      Specify the Number of Bytes

      The -c num or -bytes=num option tells head to display num bytes from the file rather than the default 10 lines.

      Specify the Number of Lines

      You can change the number of lines displayed with the -n num or -lines=num option.

      Viewing the Ends of Files with tail

      The tail command works just like head, except that tail displays the last 10 lines of a file. (You can use the -c or -bytes, and -n or -lines options to change the amount of data displayed, just as with head.) This command is useful for examining recent activity in log files or other files to which data may be appended.

      The tail command supports several options that aren't present in head and that enable the program to handle additional duties, including the following:

      Track a File

      The -f or -follow option tells tail to keep the file open and to display new lines as they're added. This feature is helpful for tracking log files because it enables you to see changes as they're made to the file.

      Stop Tracking on Program Termination

      The -pid=pid option tells tail to terminate tracking (as initiated by -f or -follow) once the process with a process ID (PID) of pid terminates. (PIDs are described in more detail in Chapter 2, “Managing Software.”)

      Some additional options provide more obscure capabilities. Consult tail's man page for details.

      You can combine head with tail to display or extract portions of a file. For instance, suppose you want to display lines 11 through 15 of a file, sample.txt. You can extract the first 15 lines of the file with head and then display the last five lines of that extraction with tail. The final command would be head – n 15 sample.txt | tail – n 5.

      Paging through Files with less

      The less command's name is a joke; it's a reference to the more command, which was an early file pager. The idea was to create a better version of more, so the developers called it less (“less is more”).

      The idea behind less (and more, for that matter) is to enable you to read a file a screen at a time. When you type less filename, the program displays the first few lines of filename. You can then page back and forth through the file:

      ● Pressing the spacebar moves forward through the file a screen at a time.

      ● Pressing Esc followed by V moves backward through the file a screen at a time.

      ● The Up and Down arrow keys move up or down through the file a line at a time.

      ● You can search the file's contents by pressing the slash (/) key followed by the search term. For instance, typing /portable finds the first occurrence of the string portable after the current position. Typing a slash followed by the Enter key moves to the next occurrence of the search term. Typing n alone repeats the search forward, while typing N alone repeats the search backward.

      ● You can search backward in the file by using the question mark (?) key rather than the slash key.

      ● You can move to a specific line by typing g followed by the line number, as in g50 to go to line 50.

      ● When you're done, type q to exit from the program.

      Unlike most of the programs described here, less can't be readily used in a pipe, except as the final command in the pipe. In that role, though, less is very useful because it enables you to examine lengthy output conveniently.

      Although less is quite common on Linux systems and is typically configured as the default text pager, some Unix-like systems use more in this role. Many of less's features, such as the ability to page backward in a file, don't work in more.

      One additional less feature can be handy: Typing h displays less's internal help system. This display summarizes the commands you may use, but it's long enough that you must use the usual less paging features to view it all! When you're done with the help screens, just type q as if you were exiting from viewing a help document with less. This action will return you to your original document.

      File-Summarizing Commands

      The final text-filtering commands described here are used to summarize text in one way or another. The cut command takes segments of an input file and sends them to standard output, while the wc command displays some basic statistics on the file.

      Extracting Text with cut

      The cut command extracts portions of input lines and displays them on standard output. You can specify what to cut from input lines in several ways:

      By Byte

      The -b list or -bytes=list option cuts the specified list of bytes from the input file. (The format of list is described shortly.)

      By Character

      The -c list or -characters=list option cuts the specified list of characters from the input file. In practice, this method and the by-byte method usually produce identical results. (If the input file uses a multibyte encoding system, though, the results won't be identical.)

      By Field

      The -f list or -fields=list option cuts the specified list of fields from the input file. By default, a field is a tab-delimited section of a line, but you can change the delimiting character with the -d char, -delim=char, or -delimiter=char option, where char is the character you want to use to delimit fields. Ordinarily, cut echoes lines that don't contain delimiters. Including the -s or -only-delimited option changes this behavior so that the program doesn't echo lines that don't contain the delimiter character.

      Many of these options take a list option, which is a way to specify multiple bytes, characters, or fields. You make this specification by number. It can be a single number (such as 4), a closed range of numbers (such as 2-4), or an open range of numbers (such as -4 or 4-). In this final case, all bytes, characters, or fields from the beginning of the line to the specified number (or from the specified number to the end of the line) are included in the list.

      Конец ознакомительного фрагмента.

      Текст предоставлен ООО «ЛитРес».

      Прочитайте эту книгу целиком, купив полную


Скачать книгу