theshell.ch/site/pages/cmd/advanced/head-tail.html
montiag 0dbd4778df advanced:paste-cat
git-svn-id: svn+ssh://atelier.inf.usi.ch/home/bevilj/group-1@247 a672b425-5310-4d7a-af5c-997e18724b81
2018-11-18 12:28:06 +00:00

52 lines
1.8 KiB
HTML

---
layout: page
author: Agostino Monti
category-page: advanced
category-title: Advanced commands
tags: head tail text file
title: head-tail
---
<br>
<br>
<h3><code>head</code></h3>
<p>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.
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. </p>
<br>
<h3><code>tail</code></h3>
<p>The tail command is similar to the head command
except that it reads the final lines in files rather than the first lines.</p>
<h4>Examples</h4>
<p><pre>
head file1.txt <br>
head file1.txt file2.txt <br>
tail fail1.txt</pre></p>
<h3>flags</h3>
<ul>
<li>The <code>-n</code> option can be used followed by an integer indicating the number of lines desired.
-n is a very tolerant option, it is not necessary for the integer to directly
follow it without a space in between. In fact, the letter <i>n</i> does not
even need to be used at all. Just the hyphen and the integer
(with no intervening space) are sufficient to tell head how many lines to return.
<h4>Examples</h4>
<pre>
head -n15 file1.txt <br>
head -n 15 file1.txt <br>
head -15 file1.txt</pre></li>
<li>The output from other commands can be sent via a pipe (represented by the vertical bar character)
to head to use as its input. The following sends the output from the ls command
to head, which, in turn, displays the first ten lines of the output that it receives from ls
<h4>Examples</h4>
<pre>
ls | head </pre></li>
</ul>