theshell.ch/site/pages/cmd/advanced/head-tail.html

43 lines
1.3 KiB
HTML

---
layout: page
author: Agostino Monti
category-page: advanced
category-title: Advanced commands
tags: head tail text file
title: head and tail
---
The <code>head</code> command reads the first few lines of any text given to it as an input
and writes them to standard output.<br>
If more than one input file is provided, head will return the first ten lines
from each file, precede each set of lines by the name of the file and separate
each set of lines by one vertical space.
<pre>
head [flags] [file1] [file2] ...
</pre>
The <code>tail</code> command is similar to the <code>head</code> command
except that it reads the final lines in files rather than the first lines.
<pre>
tail [flags] [file1] [file2] ...
</pre>
<h3>Flags</h3>
<ul>
<li><b>-n</b>: can be used followed by number, which indicates the number of
lines desired in the output. This flag can be used without <i>n</i>: the hyphen and the
number (with no intervening space) are enough to tell head how many lines to return.
<pre>
head -n15 file1.txt <br>
head -n 15 file1.txt <br>
head -15 file1.txt</pre>
</li>
<li><b>-c</b>: similar to <i>-n</i> with the only difference being that the number stands for
bytes instead of the number of lines and the fact that it can't be used without explicitly
typing the <i>-c</i>.
</li>
</ul>