2018-11-18 12:28:06 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
author: Agostino Monti
|
|
|
|
category-page: advanced
|
|
|
|
category-title: Advanced commands
|
|
|
|
tags: head tail text file
|
2018-11-18 15:30:51 +00:00
|
|
|
title: head and tail
|
2018-11-18 12:28:06 +00:00
|
|
|
---
|
|
|
|
|
2018-11-18 15:30:51 +00:00
|
|
|
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.
|
2018-11-18 12:28:06 +00:00
|
|
|
|
2018-11-18 15:30:51 +00:00
|
|
|
<pre>
|
|
|
|
head [flags] [file1] [file2] ...
|
|
|
|
</pre>
|
2018-11-18 12:28:06 +00:00
|
|
|
|
2018-11-18 15:30:51 +00:00
|
|
|
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.
|
2018-11-18 12:28:06 +00:00
|
|
|
|
2018-11-18 15:30:51 +00:00
|
|
|
<pre>
|
|
|
|
tail [flags] [file1] [file2] ...
|
|
|
|
</pre>
|
2018-11-18 12:28:06 +00:00
|
|
|
|
2018-11-18 15:30:51 +00:00
|
|
|
<h3>Flags</h3>
|
2018-11-18 12:28:06 +00:00
|
|
|
|
2018-11-18 15:30:51 +00:00
|
|
|
<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>
|