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

5. Output of parts of files

These commands output pieces of the input.

5.1 head: Output the first part of files  Output the first part of files.
5.2 tail: Output the last part of files  Output the last part of files.
5.3 split: Split a file into fixed-size pieces  Split a file into fixed-size pieces.
5.4 csplit: Split a file into context-determined pieces  Split a file into context-determined pieces.


[ < ] [ > ]   [ << ] [ 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 2. Common options.

`-c bytes'
`--bytes=bytes'
Print the first bytes bytes, instead of initial lines. Appending `b' multiplies bytes by 512, `k' by 1024, and `m' by 1048576.

`-n n'
`--lines=n'
Output the first n lines.

`-q'
`--quiet'
`--silent'
Never print file name headers.

`-v'
`--verbose'
Always print file name headers.

On older systems, head supports an obsolete option `-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'). POSIX 1003.1-2001 (see section 2.5 Standards conformance) does not allow this; use `-c count' or `-n count' instead.


[ < ] [ > ]   [ << ] [ 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.

If any option-argument is a number n starting with a `+', tail begins printing with the nth item from the start of each file, instead of from the end.

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

`-c bytes'
`--bytes=bytes'
Output the last bytes bytes, instead of final lines. Appending `b' multiplies bytes by 512, `k' by 1024, and `m' by 1048576.

`-f'
`--follow[=how]'
Loop forever trying to read more characters at the end of the file, presumably because the file is growing. This option is ignored when reading from a pipe. 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 by reopening it periodically to see if it has been removed and recreated by some other program.

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'.

`-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 meaningful only when following by 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 Historical implementations of tail have required that number be an integer. However, GNU tail accepts an arbitrary floating point number.

`--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 size has remained the same, 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 n'
`--lines=n'
Output the last n lines.

`-q'
`--quiet'
`--silent'
Never print file name headers.

`-v'
`--verbose'
Always print file name headers.

On older systems, tail supports an obsolete option `-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 (`cfqv'). Some older tail implementations also support an obsolete option `+count' with the same meaning as `-+count'. POSIX 1003.1-2001 (see section 2.5 Standards conformance) does not allow these options; use `-c count' or `-n count' instead.


[ < ] [ > ]   [ << ] [ 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 letters (`aa', `ab', ... by default), such that concatenating the output files in 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 2. Common options.

`-a length'
`--suffix-length=length'
Use suffixes of length length. The default length is 2.

`-l lines'
`--lines=lines'
Put lines lines of input into each output file.

On older systems, split supports an obsolete option `-lines'. POSIX 1003.1-2001 (see section 2.5 Standards conformance) does not allow this; use `-l lines' instead.

`-b bytes'
`--bytes=bytes'
Put the first bytes bytes of input into each output file. Appending `b' multiplies bytes by 512, `k' by 1024, and `m' by 1048576.

`-C bytes'
`--line-bytes=bytes'
Put into each output file as many complete lines of input as possible without exceeding bytes bytes. For lines longer than bytes bytes, put bytes bytes into each output file until less than bytes bytes of the line are left, then continue normally. bytes has the same format as for the `--bytes' option.

`--verbose'
Write a diagnostic to standard error just before each output file is opened.


[ < ] [ > ]   [ << ] [ 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 line 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 a `+' or `-' followed by a positive integer. If it is given, the input up to 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. 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 filename 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 2. 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.


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

This document was generated by Jeff Bailey on December, 28 2002 using texi2html