[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5. Output of parts of files

These commands output pieces of the input.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.1 head: Output the first part of files

head prints the first part (10 lines by default) of each file; it reads from standard input if no files are given or when given a file of `-'. Synopsis:

 
head [option]… [file]…

If more than one file is specified, head prints a one-line header consisting of:

 
==> file name <==

before the output for each file.

The program accepts the following options. Also see Common options.

`-c k'
`--bytes=k'

Print the first k bytes, instead of initial lines. However, if k starts with a `-', print all but the last k bytes of each file. k may be, or may be an integer optionally followed by, one of the following multiplicative suffixes:

 
`b'  =>            512 ("blocks")
`KB' =>           1000 (KiloBytes)
`K'  =>           1024 (KibiBytes)
`MB' =>      1000*1000 (MegaBytes)
`M'  =>      1024*1024 (MebiBytes)
`GB' => 1000*1000*1000 (GigaBytes)
`G'  => 1024*1024*1024 (GibiBytes)

and so on for `T', `P', `E', `Z', and `Y'.

`-n k'
`--lines=k'

Output the first k lines. However, if k starts with a `-', print all but the last k lines of each file. Size multiplier suffixes are the same as with the `-c' option.

`-q'
`--quiet'
`--silent'

Never print file name headers.

`-v'
`--verbose'

Always print file name headers.

For compatibility head also supports an obsolete option syntax `-countoptions', which is recognized only if it is specified first. count is a decimal number optionally followed by a size letter (`b', `k', `m') as in `-c', or `l' to mean count by lines, or other option letters (`cqv'). Scripts intended for standard hosts should use `-c count' or `-n count' instead. If your script must also run on hosts that support only the obsolete syntax, it is usually simpler to avoid head, e.g., by using `sed 5q' instead of `head -5'.

An exit status of zero indicates success, and a nonzero value indicates failure.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.2 tail: Output the last part of files

tail prints the last part (10 lines by default) of each file; it reads from standard input if no files are given or when given a file of `-'. Synopsis:

 
tail [option]… [file]…

If more than one file is specified, tail prints a one-line header consisting of:

 
==> file name <==

before the output for each file.

GNU tail can output any amount of data (some other versions of tail cannot). It also has no `-r' option (print in reverse), since reversing a file is really a different job from printing the end of a file; BSD tail (which is the one with `-r') can only reverse files that are at most as large as its buffer, which is typically 32 KiB. A more reliable and versatile way to reverse files is the GNU tac command.

The program accepts the following options. Also see Common options.

`-c k'
`--bytes=k'

Output the last k bytes, instead of final lines. However, if k starts with a `+', start printing with the kth byte from the start of each file, instead of from the end. k may be, or may be an integer optionally followed by, one of the following multiplicative suffixes:

 
`b'  =>            512 ("blocks")
`KB' =>           1000 (KiloBytes)
`K'  =>           1024 (KibiBytes)
`MB' =>      1000*1000 (MegaBytes)
`M'  =>      1024*1024 (MebiBytes)
`GB' => 1000*1000*1000 (GigaBytes)
`G'  => 1024*1024*1024 (GibiBytes)

and so on for `T', `P', `E', `Z', and `Y'.

`-f'
`--follow[=how]'

Loop forever trying to read more characters at the end of the file, presumably because the file is growing. If more than one file is given, tail prints a header whenever it gets output from a different file, to indicate which file that output is from.

There are two ways to specify how you'd like to track files with this option, but that difference is noticeable only when a followed file is removed or renamed. If you'd like to continue to track the end of a growing file even after it has been unlinked, use `--follow=descriptor'. This is the default behavior, but it is not useful if you're tracking a log file that may be rotated (removed or renamed, then reopened). In that case, use `--follow=name' to track the named file, perhaps by reopening it periodically to see if it has been removed and recreated by some other program. Note that the inotify-based implementation handles this case without the need for any periodic reopening.

No matter which method you use, if the tracked file is determined to have shrunk, tail prints a message saying the file has been truncated and resumes tracking the end of the file from the newly-determined endpoint.

When a file is removed, tail's behavior depends on whether it is following the name or the descriptor. When following by name, tail can detect that a file has been removed and gives a message to that effect, and if `--retry' has been specified it will continue checking periodically to see if the file reappears. When following a descriptor, tail does not detect that the file has been unlinked or renamed and issues no message; even though the file may no longer be accessible via its original name, it may still be growing.

The option values `descriptor' and `name' may be specified only with the long form of the option, not with `-f'.

The `-f' option is ignored if no file operand is specified and standard input is a FIFO or a pipe. Likewise, the `-f' option has no effect for any operand specified as `-', when standard input is a FIFO or a pipe.

`-F'

This option is the same as `--follow=name --retry'. That is, tail will attempt to reopen a file when it is removed. Should this fail, tail will keep trying until it becomes accessible again.

`--retry'

This option is useful mainly when following by name (i.e., with `--follow=name'). Without this option, when tail encounters a file that doesn't exist or is otherwise inaccessible, it reports that fact and never checks it again.

`--sleep-interval=number'

Change the number of seconds to wait between iterations (the default is 1.0). During one iteration, every specified file is checked to see if it has changed size. Historical implementations of tail have required that number be an integer. However, GNU tail accepts an arbitrary floating point number (using a period before any fractional digits).

`--pid=pid'

When following by name or by descriptor, you may specify the process ID, pid, of the sole writer of all file arguments. Then, shortly after that process terminates, tail will also terminate. This will work properly only if the writer and the tailing process are running on the same machine. For example, to save the output of a build in a file and to watch the file grow, if you invoke make and tail like this then the tail process will stop when your build completes. Without this option, you would have had to kill the tail -f process yourself.

 
$ make >& makerr & tail --pid=$! -f makerr

If you specify a pid that is not in use or that does not correspond to the process that is writing to the tailed files, then tail may terminate long before any files stop growing or it may not terminate until long after the real writer has terminated. Note that `--pid' cannot be supported on some systems; tail will print a warning if this is the case.

`--max-unchanged-stats=n'

When tailing a file by name, if there have been n (default n=5) consecutive iterations for which the file has not changed, then open/fstat the file to determine if that file name is still associated with the same device/inode-number pair as before. When following a log file that is rotated, this is approximately the number of seconds between when tail prints the last pre-rotation lines and when it prints the lines that have accumulated in the new log file. This option is meaningful only when following by name.

`-n k'
`--lines=k'

Output the last k lines. However, if k starts with a `+', start printing with the kth line from the start of each file, instead of from the end. Size multiplier suffixes are the same as with the `-c' option.

`-q'
`--quiet'
`--silent'

Never print file name headers.

`-v'
`--verbose'

Always print file name headers.

For compatibility tail also supports an obsolete usage `tail -[count][bcl][f] [file]', which is recognized only if it does not conflict with the usage described above. This obsolete form uses exactly one option and at most one file. In the option, count is an optional decimal number optionally followed by a size letter (`b', `c', `l') to mean count by 512-byte blocks, bytes, or lines, optionally followed by `f' which has the same meaning as `-f'.

On older systems, the leading `-' can be replaced by `+' in the obsolete option syntax with the same meaning as in counts, and obsolete usage overrides normal usage when the two conflict. This obsolete behavior can be enabled or disabled with the _POSIX2_VERSION environment variable (see section Standards conformance).

Scripts intended for use on standard hosts should avoid obsolete syntax and should use `-c count[b]', `-n count', and/or `-f' instead. If your script must also run on hosts that support only the obsolete syntax, you can often rewrite it to avoid problematic usages, e.g., by using `sed -n '$p'' rather than `tail -1'. If that's not possible, the script can use a test like `if tail -c +1 </dev/null >/dev/null 2>&1; then …' to decide which syntax to use.

Even if your script assumes the standard behavior, you should still beware usages whose behaviors differ depending on the POSIX version. For example, avoid `tail - main.c', since it might be interpreted as either `tail main.c' or as `tail -- - main.c'; avoid `tail -c 4', since it might mean either `tail -c4' or `tail -c 10 4'; and avoid `tail +4', since it might mean either `tail ./+4' or `tail -n +4'.

An exit status of zero indicates success, and a nonzero value indicates failure.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3 split: Split a file into fixed-size pieces

split creates output files containing consecutive sections of input (standard input if none is given or input is `-'). Synopsis:

 
split [option] [input [prefix]]

By default, split puts 1000 lines of input (or whatever is left over for the last section), into each output file.

The output files' names consist of prefix (`x' by default) followed by a group of characters (`aa', `ab', … by default), such that concatenating the output files in traditional sorted order by file name produces the original input file. If the output file names are exhausted, split reports an error without deleting the output files that it did create.

The program accepts the following options. Also see Common options.

`-l lines'
`--lines=lines'

Put lines lines of input into each output file.

For compatibility split also supports an obsolete option syntax `-lines'. New scripts should use `-l lines' instead.

`-b size'
`--bytes=size'

Put size bytes of input into each output file. size may be, or may be an integer optionally followed by, one of the following multiplicative suffixes:

 
`b'  =>            512 ("blocks")
`KB' =>           1000 (KiloBytes)
`K'  =>           1024 (KibiBytes)
`MB' =>      1000*1000 (MegaBytes)
`M'  =>      1024*1024 (MebiBytes)
`GB' => 1000*1000*1000 (GigaBytes)
`G'  => 1024*1024*1024 (GibiBytes)

and so on for `T', `P', `E', `Z', and `Y'.

`-C size'
`--line-bytes=size'

Put into each output file as many complete lines of input as possible without exceeding size bytes. Individual lines longer than size bytes are broken into multiple files. size has the same format as for the `--bytes' option.

`-a length'
`--suffix-length=length'

Use suffixes of length length. The default length is 2.

`-d'
`--numeric-suffixes'

Use digits in suffixes rather than lower-case letters.

`--verbose'

Write a diagnostic just before each output file is opened.

An exit status of zero indicates success, and a nonzero value indicates failure.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.4 csplit: Split a file into context-determined pieces

csplit creates zero or more output files containing sections of input (standard input if input is `-'). Synopsis:

 
csplit [option]… input pattern

The contents of the output files are determined by the pattern arguments, as detailed below. An error occurs if a pattern argument refers to a nonexistent line of the input file (e.g., if no remaining line matches a given regular expression). After every pattern has been matched, any remaining input is copied into one last output file.

By default, csplit prints the number of bytes written to each output file after it has been created.

The types of pattern arguments are:

`n'

Create an output file containing the input up to but not including line n (a positive integer). If followed by a repeat count, also create an output file containing the next n lines of the input file once for each repeat.

`/regexp/[offset]'

Create an output file containing the current line up to (but not including) the next line of the input file that contains a match for regexp. The optional offset is an integer. If it is given, the input up to (but not including) the matching line plus or minus offset is put into the output file, and the line after that begins the next section of input.

`%regexp%[offset]'

Like the previous type, except that it does not create an output file, so that section of the input file is effectively ignored.

`{repeat-count}'

Repeat the previous pattern repeat-count additional times. The repeat-count can either be a positive integer or an asterisk, meaning repeat as many times as necessary until the input is exhausted.

The output files' names consist of a prefix (`xx' by default) followed by a suffix. By default, the suffix is an ascending sequence of two-digit decimal numbers from `00' to `99'. In any case, concatenating the output files in sorted order by file name produces the original input file.

By default, if csplit encounters an error or receives a hangup, interrupt, quit, or terminate signal, it removes any output files that it has created so far before it exits.

The program accepts the following options. Also see Common options.

`-f prefix'
`--prefix=prefix'

Use prefix as the output file name prefix.

`-b suffix'
`--suffix=suffix'

Use suffix as the output file name suffix. When this option is specified, the suffix string must include exactly one printf(3)-style conversion specification, possibly including format specification flags, a field width, a precision specifications, or all of these kinds of modifiers. The format letter must convert a binary integer argument to readable form; thus, only `d', `i', `u', `o', `x', and `X' conversions are allowed. The entire suffix is given (with the current output file number) to sprintf(3) to form the file name suffixes for each of the individual output files in turn. If this option is used, the `--digits' option is ignored.

`-n digits'
`--digits=digits'

Use output file names containing numbers that are digits digits long instead of the default 2.

`-k'
`--keep-files'

Do not remove output files when errors are encountered.

`-z'
`--elide-empty-files'

Suppress the generation of zero-length output files. (In cases where the section delimiters of the input file are supposed to mark the first lines of each of the sections, the first output file will generally be a zero-length file unless you use this option.) The output file sequence numbers always run consecutively starting from 0, even when this option is specified.

`-s'
`-q'
`--silent'
`--quiet'

Do not print counts of output file sizes.

An exit status of zero indicates success, and a nonzero value indicates failure.

Here is an example of its usage. First, create an empty directory for the exercise, and cd into it:

 
$ mkdir d && cd d

Now, split the sequence of 1..14 on lines that end with 0 or 5:

 
$ seq 14 | csplit - '/[05]$/' '{*}'
8
10
15

Each number printed above is the size of an output file that csplit has just created. List the names of those output files:

 
$ ls
xx00  xx01  xx02

Use head to show their contents:

 
$ head xx*
==> xx00 <==
1
2
3
4

==> xx01 <==
5
6
7
8
9

==> xx02 <==
10
11
12
13
14

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on January, 20 2010 using texi2html 1.76.